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
3
  • 1
    Does message happen to contain quotes, newlines or other "special" characters? Commented Jan 26, 2022 at 13:01
  • @Thomas can you provide a list of special characters? It looks like you are right. Commented Jan 26, 2022 at 13:08
  • @Thomas message contains a quote character written as \' Commented Jan 26, 2022 at 13:25

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

it's difficult for me to understand this behavior, where exactly did I go wrong ?
moreover, I run the code that I have given, with multiple combination of special characters and it did work on those occasions as well.
Do you understand why arguments[0].value='O'Hara' is a problem?
-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

9 Comments

It does not address the problem. If the original code gives a syntax error, so will this one.
@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.
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: See by yourself, updated with screenshot.
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.