Home > Software engineering >  JavaScriptException in execute_script method
JavaScriptException in execute_script method

Time:01-27

def send(self, message):
    try:
        textArea = self.driver.find_element_by_xpath("//textarea[@placeholder='Message...']")
        textArea.clear()
        self.driver.execute_script("arguments[0].value='"   message   "'", textArea) # Error right here
        textArea.send_keys(Keys.SPACE)
        sendButton = self.driver.find_element_by_xpath("//button[contains(text(), 'Send')]").click()

    except NoSuchElementException or StaleElementReferenceException as ex:
        print("Исключение в send()")
        print(ex)

Up to this point, the fifth line worked, but now I just can’t figure out what the problem is. And the variables have correct values.

File "D:\PROJECTS\BLD_Project\main.py", line 187, in send
    self.driver.execute_script("arguments[0].value='"   message   "'", textArea)
  File "D:\PROJECTS\INST\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 878, in execute_script
    return self.execute(command, {
  File "D:\PROJECTS\INST\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "D:\PROJECTS\INST\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Unexpected identifier

CodePudding user response:

You seem to be running into escaping troubles. If message contains a quote character, for example O'Hara, you'd end up with a syntax error in the executed JavaScript code:

arguments[0].value='O'Hara'

The enter image description here

  •  Tags:  
  • Related