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:)
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.