0

I am inquiring about executing javascript in the browser with selenium. I've seen plenty of ways how to do this, however, saving the output to a variable seems to be something else.

r = driver.execute_script("`; ${document.cookie}`.split(`; bearer_token=`).pop().split(';').shift()")

This makes R = None, which is not the output of the code.

JamCheats
  • 21
  • 1
  • 4

2 Answers2

0

You have to add the return statement:

r = driver.execute_script("return 2+2")
print(r)

will print 4.

And it will print None without return.

Max Daroshchanka
  • 2,698
  • 2
  • 10
  • 14
-1

execute_script() function in the selenium works for executing something particularly in the specific browser tab only, I am sure that the outputs won't be stored in a python variable. Instead I recommend to save the cookies in your local dir and later perform your tasks.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 06 '22 at 09:12