Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda