Home > Mobile >  Finding all possible button elements in Selenium python
Finding all possible button elements in Selenium python

Time:04-21

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

CodePudding user response:

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

CodePudding user response:

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