Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading