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

238
Visualizações
Using JavaScript, how do I get this code to autofill Username & click button, delay, or wait, then autofill Password & click log in?
document.getElementsByName("Username")[0].value = 'FamilyGuy'

function myFunction(){
    //pag 1, delay a second. Giving user name time to fill
    document.getElementById("NextButton").click();
}
//delay
setTimeout(myFunction, 1000);

function myFunction(){
    //page 2, delay a second. Giving passwrd time to fill befor logging in
    document.getElementsByName("Password")[0].value = 'this1sth3p4ssw0rd!'
}
//delay
setTimeout(myFunction, 1000);

document.getElementById("LoginButton").click();

//I am using this extension using this extension
[1]: https://i.stack.imgur.com/LNMiJ.png

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

setTimeout is an asynchronous function. It doesn't pause the execution of code that follows it.

This means that, in your example code, the two timeouts will run simultaneously, and your login button will be pressed immediately. To fix this, put the second timeout at the end of your first function, rather than in the main body of code. Also put the pressing of the login button inside the second function.

document.getElementsByName("Username")[0].value = 'FamilyGuy'


function myFunctionB(){
    //page 2, delay a second. Giving passwrd time to fill befor logging in
    document.getElementsByName("Password")[0].value = 'this1sth3p4ssw0rd!'
    document.getElementById("LoginButton").click();
}

function myFunctionA(){
    //pag 1, delay a second. Giving user name time to fill
    document.getElementById("NextButton").click();
    
    //delay
    setTimeout(myFunctionB, 1000);
}

//delay
setTimeout(myFunctionA, 1000);

I don't know the way the NextButton operates, but the use of a delay to wait until the Username value changes is probably unnecessary. Consider using the following code:

function myFunction(){
    document.getElementsByName("Password")[0].value = 'this1sth3p4ssw0rd!'
    document.getElementById("LoginButton").click();
}

document.getElementsByName("Username")[0].value = 'FamilyGuy'
document.getElementById("NextButton").click();
    
//delay
setTimeout(myFunction, 1000);
about 4 years ago · Juan Pablo Isaza Relatório

0

This may be one possible implementation to achieve the below-described (what is assumed to be the desired) objective:

  1. A screen with input-boxes username, password and buttons 'Next', 'Login' is rendered
  2. After waiting for some time (say 1.5 seconds), username is auto-populated
  3. Another delay of 2 seconds and 'Next' button click is programatically triggered
  4. After delay of 2 seconds, password is auto-populated
  5. Finally, another delay of 1 second and 'Login' button is programatically triggered.

The time-delays are just randomly assigned in the snippet and may be altered as required.

It would definitely helpful to understand the context within which this is being used - to cater a more suitable answer.

const btnClicked = name => console.log(`${name} button clicked `);
setTimeout(
 () => {
  document.getElementById("Username").value = 'FamilyGuy';
  setTimeout(
    () => {
      document.getElementById("NextButton").click();
      setTimeout(
        () => {
          document.getElementById("Password").value = 'this1sth3p4ssw0rd!';
          setTimeout(
            () => {
              document.getElementById("LoginButton").click();
            },
            1000
          )
        },
        2000
      )
    },
    2000
  )
 },
 1500
);
<html>
<body>
<input id='Username' />
<button id='NextButton' onclick='btnClicked("Next")'>Next</button>
<input id='Password' />
<button id='LoginButton' onclick='btnClicked("Login")'>Login</button>
<script src='script.js'></script>
</body>
</html>

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