0

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 a 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?

vancouver3
  • 51
  • 1
  • 9

2 Answers2

2

Upgrading your Python version will not help solve this issue, Since find_element is 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 extra (

and the second one

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

seems correct.

cruisepandey
  • 26,802
  • 6
  • 16
  • 37
1

The correct syntax of

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

With new style will be

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

The general syntaxes are:

driver.find_element_by_xpath(xpath_locator_string)

and

driver.find_element(By.XPATH, xpath_locator_string)
Prophet
  • 21,440
  • 13
  • 47
  • 64