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

177
Views
Use a function inside browser.execute_script() (Selenium, Python)

I'm trying to use a javascript code inside the console with selenium python, but I don't know how. This is the function:

function login(token) {
  setInterval(() => {
    document.body
            .appendChild(document.createElement `iframe`)
            .contentWindow.localStorage.token = `"${token}"`
  }, 50);
  setTimeout(() => {
    location.reload();
  }, 2500);
}
    
login(token);

I'm truing to use the function inside browser.execute_script() but I don't know how to add it.

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

0

You could write a function that creates a script and returns it as a string. You can then call driver.execute_script() to run the script when needed.

This should work:

def generate_login_script(token):
    script = """
setInterval(()=>{{document.body.appendChild(document.createElement `iframe`)
.contentWindow.localStorage.token="{token}"}},50);
setTimeout(()=>{{location.reload()}},2500);
    """.format(token=token)
    return script

driver = webdriver.Chrome("D:\chromedriver\94\chromedriver.exe")
driver.get("https://www.youwebsite.com")

driver.execute_script(generate_login_script("testToken"))

I have tested this on thefamoussearchenginethatstartswithG.com and it works.

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!