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.

2
  • 1
    What error are you getting ? Commented Mar 24, 2018 at 9:02
  • try //div[contains(.,'5') and contains(@class, 'option')] Commented Mar 24, 2018 at 11:11

2 Answers 2

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')]")
Sign up to request clarification or add additional context in comments.

Comments

1

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

driver.find_element(By.XPATH, '//input[@name="name" and @value="value"]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.