Selenium js execution as string went wrong [Python]

by Quanti Monati   Last Updated May 15, 2019 17:26 PM

I'm trying to execute following command:

driver.execute_script("document.getElementById('textfield').value = 'string';")

And everything works as it should. But when I try to substitute 'string' with generated string - I'm getting error.

selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected identifier
(Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17763 x86_64)

I've tried to to this like this:

driver.execute_script("document.getElementById('textfield').value = %s;" % "huge generated string")

Also, this way:

driver.execute_script("document.getElementById('textfield').value = {str};".format(str="huge generated string"))

But I/m getting error anyways.

Maybe, you can give me advice - what I'm doing wrong?



Related Questions




python selenium click on survey button error

Updated July 04, 2018 20:26 PM