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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vancouver3
  • 49
  • 2
  • 11

2 Answers2

2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
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
  • 32,350
  • 22
  • 54
  • 79