i am trying to excute this script using python selenium but i can't because of the format of script how can i edit it to work with python
driver.execute_script("parent.postMessage(JSON.stringify({
eventId: "challenge-complete",
payload: {
sessionToken: 'YOUR_TOKEN'
}
}), "*"))")
^
SyntaxError: unterminated string literal (detected at line 40)
CodePudding user response:
There were a few issues...
You are using double quotes inside of other double quotes which closes the string early. You should use single quotes to define strings inside of a string enclosed in double quotes. A simple example of this would be
"A string with "enclosed quotes" will not work."
This is interpreted as, "A string with ", since it's the first string enclosed with double quotes. A simple way to fix this is to use
"A string with 'enclosed quotes' will work."
You were missing quotes around the string inside the
JSON.stringify()
call. Since you have three levels of nested quotes, you'll have to escape one of them. I chose to escape this level as\"
.You had an extra close parenthesis.
The updated code should be
driver.execute_script("parent.postMessage(JSON.stringify(\"{
eventId: 'challenge-complete',
payload: {
sessionToken: 'YOUR_TOKEN'
}
}\"), '*')")
CodePudding user response:
You have one too many closing parenthesis ')'
You will need to remove the unneeded one:
driver.execute_script("parent.postMessage(JSON.stringify({
eventId: "challenge-complete",
payload: {
sessionToken: 'YOUR_TOKEN'
}
}), "*")")