Selenium selects elements based on the tag name

Reference learning address: http://www.python3.vip/tut/auto/selenium/02/#%E6%A0%B9%E6%8D%AE-class%E5%B1%9E%E6%80%A7- %E9%80%89%E6%8B%A9%E5%85%83%E7%B4%A0

Similarly, we can use the method find_elements_by_tag_name to select all elements whose tag name is div, as shown below

Insert picture description here
Similarly, we can use the method find_elements_by_tag_name to select all elements whose tag name is div, as shown below

from selenium import webdriver

# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'D:\tools-work\chromedriver_win32\chromedriver.exe')

# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://cdn1.python3.vip/files/selenium/sample1.html')

# 根据 tag name 选择元素,返回的是 一个列表
# 里面 都是 tag 名为 div 的元素对应的 WebElement对象
elements = wd.find_elements_by_tag_name('div')

# 取出列表中的每个 WebElement对象,打印出其text属性的值
# text属性就是该 WebElement对象对应的元素在网页中的文本内容
for element in elements:
    print(element.text)

find_element 和 find_elements 的区别

Use find_elements to select all elements that meet the conditions, if there are no elements that meet the conditions, return an empty list

Use find_element to select the first element that meets the condition, if there is no element that meets the condition, throw NoSuchElementException

Guess you like

Origin blog.csdn.net/weixin_41665637/article/details/111315408