2

I want to find specific web element by executing javascript code and then click on that element. This is my code:

driver.get('https://justjoin.it')
driver.maximize_window()


for position in [x.text for x in driver.find_elements_by_xpath('//div[@class="css-1x9zltl"]')]:
      javascript = f"document.evaluate('//div[contains(@class, 'css-1x9zltl') and text()='{position}']', document);"
      driver.execute_script(javascript).click()

And when I run this code console throws me this error:

selenium.common.exceptions.JavascriptException: Message: javascript error: missing ) after argument list

Any ideas how to solve this?

1 Answer 1

1

It probably has to do with the interaction of the single and double quote marks within your f-string. Try escaping one by changing your javascript to:

javascript = f"document.evaluate(\"//div[contains(@class, 'css-1x9zltl') and text()='{position}']\", document);"

and see if it works.

2
  • Thanks man, first part works perfectly. But I can't click the element ;/ Do you know how to do that?
    – beginsql
    Commented Jul 19, 2020 at 21:11
  • @beginsql I'm afraid not; that is a straight selenium problem. You should probably post it as a separate question. Commented Jul 19, 2020 at 21:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.