Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda