Home > Software engineering >  Can't execute java script by using python because of arch : /
Can't execute java script by using python because of arch : /

Time:09-09

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...

  1. 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."
    
  2. 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 \".

  3. 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'
                                                    }
                                                    }), "*")")
  • Related