9

I have the following code in selenium but continue to get a syntax error. I'm trying to select an element based on multiple conditions.

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and [contains(@class, 'option')]]")$

Thanks for any help you can give.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Noah Ratliff
  • 197
  • 2
  • 2
  • 6

2 Answers2

12

As per the xpath you have shared as follows :

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and [contains(@class, 'option')]]")$

You need to consider a few facts :

  • The multiple conditions for selecting the <div> tag can't be within nested []. Either you have to specify within one [] or within multiple []s.
  • The xpath shouldn't end with unwanted characters e.g $

Solution

You can rewrite the xpath in either of the following ways :

choices = driver.find_elements_by_xpath("//div[contains(.,'5') and contains(@class, 'option')]")
# or
choices = driver.find_elements_by_xpath("//div[contains(.,'5')][contains(@class, 'option')]")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

Here's an example without using "contains()":

driver.find_element(By.XPATH, '//input[@name="name" and @value="value"]')
grantr
  • 878
  • 8
  • 16