Antarktis
Antarktis

Reputation: 163

find element by id selenium in python

In selenium when I search for an id or an id in an xpath, I am instantly met with a syntax error. For example, if I run

driver.find_element_by_xpath("//*[@id="select-dance"]/option[2]").click()

I immediately get an error

   driver.find_element_by_xpath("//*[@id="select-dance"]/option[2]").click()
                                                ^
SyntaxError: invalid syntax

I tried saving "select-dance" to a variable and then putting that variable in, but that does not help either.

Upvotes: 0

Views: 796

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193078

This error message...

SyntaxError: invalid syntax

...implies that the Locator Strategy which you have adapted have a Syntax error.

You have to either provide the entire XPath within double quotes (i.e. "...") and the attribute values within single quotes (i.e. '...') as follows:

driver.find_element_by_xpath("//*[@id='select-dance']/option[2]").click()

Or you have to provide the entire XPath within single quotes (i.e. '...') and the attribute values within double quotes (i.e. "...") as follows:

driver.find_element_by_xpath('//*[@id="select-dance"]/option[2]').click()

Upvotes: 2

Related Questions