0
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

2 Answers2

2

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 solution is to use the arguments array that you're already using, to pass the message string as well:

self.driver.execute_script("arguments[0].value = arguments[1]", textArea, message)
Thomas
  • 174,939
  • 50
  • 355
  • 478
-1

Assuming message is a string, you can use setAttribute inside execute_script like below:

textArea = self.driver.find_element_by_xpath("//textarea[@placeholder='Message...']")
self.driver.execute_script(f"arguments[0].setAttribute('value', '{message}')", textArea)

to pass message variable into textArea web element.

Update:

enter image description here

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • It does not address the problem. If the original code gives a syntax error, so will this one. – Thomas Jan 27 '22 at 09:41
  • @Thomas: I don't see any syntax error, I made a sample.html file with an input tag and could update the input field with my selenium script having execute_script, whatever I have mentioned above. It did work and did not throw any syntax error. – cruisepandey Jan 27 '22 at 09:52
  • If you didn't see any syntax error, you were failing to reproduce OP's issue in the first place. Read the question again. What happened if you set `message = "O'Hara"` in your Python code? – Thomas Jan 27 '22 at 09:55
  • @Thomas: See by yourself, updated with screenshot. – cruisepandey Jan 27 '22 at 09:56
  • he had problem at this line `self.driver.execute_script("arguments[0].value='" + message + "'", textArea) # Error right here` and I could resolve this, I am not here to make you agree on this, but I don't see anyway you should downvote this answer. Cause this actually works. – cruisepandey Jan 27 '22 at 09:58
  • It works _exactly as well as the original code_. Not worse, but also not better. Which is: fine if `message` is `This is stackoverflow`, and broken if `message` is `O'Hara`. – Thomas Jan 27 '22 at 10:00
  • @Thomas: Again updated! with multiple special character combinations. – cruisepandey Jan 27 '22 at 10:01
  • The screenshot is meaningless; the syntax error is not in the Python code. `arguments[0].setAttribute('value', 'This is stack'ove'rfl'ow')` is now the JavaScript you're producing. Which will fail as soon as the browser tries to run it. (Here ends my will to discuss this further.) – Thomas Jan 27 '22 at 10:05
  • @Thomas: that's the point, it did work with my sample.html file but anyway, I can not spend my precious time on this. You are right ! – cruisepandey Jan 27 '22 at 10:06