在Selenium Python中找到所有可能的按钮元素

发布于 2025-01-22 19:04:57 字数 633 浏览 2 评论 0 原文

我正在尝试从网站上获取所有按钮,但是似乎硒语法已经更改,而没有更新文档。我正在尝试从网站上获取按下的按钮:

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 被贬低了。有人可以帮忙吗?谢谢

I am trying to get all buttons from a website but it seems the Selenium syntax has changed without the docs being updated. I am trying to get the buttons from the website as follows:

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"))

However I get the following error:

TypeError: 'str' object is not callable

As mentioned the docs still say to use find_element_by_tag_name which is depreciated. Can someone help please. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2025-01-29 19:04:57

find_element_by _*命令是 efferciated 现在。

要查找所有 < 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”)
     

find_element_by_* commands are depreciated now.

To find all the <button> elements you can use the following locator strategies:

  • Using tag_name:

    buttons = driver.find_elements(By.TAG_NAME, "button")
    
  • Using css_selector:

    buttons = driver.find_elements(By.CSS_SELECTOR, "button")
    
  • Using xpath:

    buttons = driver.find_elements(By.XPATH, "//button")
    
几度春秋 2025-01-29 19:04:57

问题是tag_name,它只是常数而不是可呼叫的方法,
文档的新用法应为:

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

在此处检查docs

Problem is with TAG_NAME it is just constant not a callable method,
new usage by docs should be:

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

Checked docs here https://www.selenium.dev/selenium/docs/api/py/index.html#example-1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文