Home > Blockchain >  How can I define a javascript variable on a webpage in selenium using Python?
How can I define a javascript variable on a webpage in selenium using Python?

Time:06-20

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 :)

CodePudding user response:

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')
  • Related