Search code examples
pythonpython-3.xseleniumselenium-webdriverdeprecation-warning

Python Selenium warning "DeprecationWarning: find_element_by_* commands are deprecated"


I have multiple elements with the same class_name (table-number). I am trying to find specific ones based on their sequence. In this case [1], the first one that appears in the DOM.

Here is working code:

my_table = driver.find_element_by_xpath("(//span[@class='table-number'])[1]").text

However, I am getting the following error:

DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead

I know I can ignore it, but it's annoying. I tried different syntax, such as:

my_table = driver.find_element(By.XPATH, ("(//span[@class='table-number'])[1]").text

my_table = driver.find_element(By.XPATH, "(//span[@class='table-number'])[1]").text

What should be correct syntax? Am I approaching it the wrong way?


Solution

  • Upgrading your Python version will not help solve this issue, since find_element is a Selenium-specific function.

    driver.find_element_by_* has been deprecated in Selenium 4 newer version.

    So you should be using

    driver.find_element(By.XPATH, "(//span[@class='table-number'])[1]").text
    

    The first one that you are using:

    my_table = driver.find_element(By.XPATH, ("(//span[@class='table-number'])[1]").text
    

    has an extra (.

    And the second one

    my_table = driver.find_element(By.XPATH, "(//span[@class='table-number'])[1]").text
    

    seems correct.