Inquire how to use execute_script on Python Selenium...

Asked 2 years ago, Updated 2 years ago, 85 views

In the code below, textarea's string is extracted using xpath and the body variable's string is used The JavaScript code that you assign to the value of the element.

And you want to run it with drv.execute_script(textarea).

Is this code I wrote grammatically correct?

I actually ran the error message "selenium.common.exception.WebDriverException: Message: unknown error: Runtime.valid or unexpected exception: SyntaxError: SyntaxError: Invalid or unexpected token"

It's coming out.

Where is the problem?

Can drv.execute_script execute only one statement?

Or is it executed even if I enter multiple JavaScript statements?

textarea = 'function getElementByXpath(path) {\
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;}\n
getElementByXpath("//*[@id=\'smart_editor2_content\']/div[4]/textarea[1]").value = "' + body + '"'

drv.execute_script(textarea)

python selenium

2022-09-22 19:35

1 Answers

An error message is a grammar error that occurs because there are characters that cannot be used in js.

Python supports multiline strings by default. Please modify the JavaScript part in the form below and try it.

temp = '''
    function showMsg(s) {
        alert(s);
    }
    showMsg("abcd");
'''
...
...
driver.execute_script(temp)


2022-09-22 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.