简体   繁体   中英

Finding all possible button elements in Selenium python

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM