teezeesak
teezeesak

Reputation: 1

Selenium doesn't recognize element by XPath

i am sorry to create a topic about this, but this little thing has been boggling my brain for the past 2 hours. Chrome returns the right element by Xpath as well as by javascript script, but Selenium tells me that in the very code, that Chrome runs perfectly fine is an error:

javascript error: missing ) after argument list

This is the code I am currently trying:

    driver.execute_script('let clickable = document.evaluate("//a[contains(@onclick,\"openFbLWin\")]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; clickable.click();')

i know its a bit messy, but overall the most important thing is //a[contains(@onclick,\"openFbLWin\")] as for the XPath selector.

Upvotes: 0

Views: 88

Answers (1)

Ayush Garg
Ayush Garg

Reputation: 2517

I think this is because when you run this in python, it converts "//a[contains(@onclick,\"openFbLWin\")]" into "//a[contains(@onclick,"openFbLWin")]", without the slashes. Then, when this is run in javascript, it can't parse the string because there is a double-quote inside of another double-quote. To fix this, change your xPath to "//a[contains(@onclick,'openFbLWin')]".

You should use selenium's inbuilt search and click tools, though. It is much more readable and faster (use find_element_by_xpath and click)

Upvotes: 1

Related Questions