Search code examples
pythonseleniumselenium-webdriverweb-scrapingfindelement

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


Solution

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