Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

430
Views
How can I define a javascript variable on a webpage in selenium using Python?

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!