我正在尝试从网站获取所有按钮,但似乎 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
,它已被折旧。有人可以帮忙吗?谢谢
问题在于 TAG_NAME 它只是常量而不是可调用方法, 文档的新用法应该是:
driver.find_element(By.TAG_NAME, 'button')
在此处检查文档https://www.selenium.dev/selenium/docs/api/py/index.html#example-1
我有一个后续问题:
登录
这是单击此 stackoverflow 页面上的登录按钮上的检查后的代码。
标签是'a',href仅包含按钮,因为帖子名称包含按钮。
还能用同样的方法找到吗?