0

So I'm trying to execute a script on a webpage in Python that just declares a js variable with selenium. My code:

driver.execute_script('var apikey = 30')

Nevertheless, when I try to call the variable it somehow says it's undeclared, really weird. How would I define a js variable on a webpage using selenium?

Have a good day :)

1 Answer 1

2

The code in the execute_script method runs as a body of an anonymous function (link).

If you need to declare a global variable, explicitly assign it to the window variable:

driver.execute_script('window.apikey = 30')
Sign up to request clarification or add additional context in comments.

Comments

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.