kostberg
kostberg

Reputation: 133

Why is selenium telling me I'm missing a paranthases in my javascript code when I'm not?

This is the code I'm currently using in my selenium application.


container_xpath = '//a[starts-with(@href, "/direct/t/")]/../../..'

def js_code(code = ""):
    return f"document.evaluate('{container_xpath}', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; {code}"

code = f'console.log(\"{js_code()}\")'
self.driver.execute_script(code)

Output of js_code():

document.evaluate('//a[starts-with(@href, "/direct/t/")]/../../..', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

This all looks fine to me, however, when I run this I'm getting an error thrown into the terminal: "selenium.common.exceptions.JavascriptException: Message: javascript error: missing ) after argument list". Ty for any help!

Upvotes: 0

Views: 51

Answers (1)

Sanjay
Sanjay

Reputation: 11

While defining a Xpath you can't use double quotes ("") so try changing it form from "/direct/t/" to '/direct/t/'

if that too doesn't seem to work then change the entre statement from

'//a[starts-with(@href, "/direct/t/")]/../../..'

to

"//a[starts-with(@href,'/direct/t/')]/../../.."

Upvotes: 1

Related Questions