To read web page elements using Python, follow these steps: Import the webdriver from the Selenium library. Launch a browser such as Chrome Driver. Use the find_element_by_* methods to find web page elements. Use element.text to read element text. Use element.get_attribute() to read element attributes. Use element.location and element.size to read element position and size.
Web page element reading guide
Web page element reading is a key task for website automation and data extraction. This article will guide you how to read the text, attributes and position of web page elements using Python and Selenium.
Import the necessary libraries
from selenium import webdriver
Start the browser
driver = webdriver.Chrome() # 或其他浏览器驱动程序
Find web page elements
Find the element using Selenium's find_element_by_*
method:
find_element_by_id("my_id")
find_element_by_name ("my_name")
find_element_by_class_name("my_class")
find_element_by_xpath("//element/path")
Read element text
text = element.text
Read element attribute
value = element.get_attribute("attribute_name")
Read element position
location = element.location # 返回 {x, y} 坐标 size = element.size # 返回 {width, height}
Practical example
Extract movie titles and ratings from the IMDb website:
# 打开 IMDb 网站 driver.get("https://www.imdb.com/") # 获取前 10 部电影的标题和评分 titles = [] ratings = [] for i in range(1, 11): # 查找标题元素 title_element = driver.find_element_by_xpath(f"(//h3)[{i}]/a") # 读标题 title = title_element.text # 查找评分元素 rating_element = driver.find_element_by_xpath(f"(//strong)[{i}]") # 读评分 rating = rating_element.text titles.append(title) ratings.append(rating) # 打印结果 for title, rating in zip(titles, ratings): print(f"{title}: {rating}")
This will print results similar to the following:
The Shawshank Redemption: 9.3 The Godfather: 9.2 The Dark Knight: 9.0 Schindler's List: 9.0 12 Angry Men: 9.0 ...
The above is the detailed content of Guide to reading web page elements. For more information, please follow other related articles on the PHP Chinese website!