在 Selenium python 中查找所有可能的按钮元素

问题描述 投票:0回答:3

我正在尝试从网站获取所有按钮,但似乎 Selenium 语法已更改而文档未更新。我正在尝试从网站获取按钮,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
url = 'https://www.programiz.com/python-programming'
driver.get(url)
buttons = driver.find_element(by=By.TAG_NAME("button"))

但是我收到以下错误:

TypeError: 'str' object is not callable

正如所提到的,文档仍然说使用

find_element_by_tag_name
,它已被折旧。有人可以帮忙吗?谢谢

python selenium selenium-webdriver web-scraping findelement
3个回答
2
投票

find_element_by_*
命令现已贬值

要查找所有

<button>
元素,您可以使用以下定位器策略

  • 使用tag_name

    buttons = driver.find_elements(By.TAG_NAME, "button")
    
  • 使用css_selector

    buttons = driver.find_elements(By.CSS_SELECTOR, "button")
    
  • 使用xpath

    buttons = driver.find_elements(By.XPATH, "//button")
    

1
投票

问题在于 TAG_NAME 它只是常量而不是可调用方法, 文档的新用法应该是:

driver.find_element(By.TAG_NAME, 'button') 

在此处检查文档https://www.selenium.dev/selenium/docs/api/py/index.html#example-1


0
投票

我有一个后续问题:

登录

这是单击此 stackoverflow 页面上的登录按钮上的检查后的代码。

标签是'a',href仅包含按钮,因为帖子名称包含按钮。

还能用同样的方法找到吗?

© www.soinside.com 2019 - 2024. All rights reserved.