Search code examples
javascriptjquerygreasemonkey-4

How to login to neopets.com through GM userscript


I created a userscript which starts at the Neopets login page and autofills the username and password. I am having trouble getting the script to click the giant green "LOG IN" button.

$('input[name=username']).val('username_here');
$('input[name=password']).val('password_here');

I have tried all of the following to click the login button or submit the form.

//None of these clicked the sign in button:
$("input.submit[name = 'welcomeLoginButton']").submit();
$("input.submit[name = 'welcomeLoginButton']").click();
$('body').on('click','welcomeLoginButton',function(){alert('logged in?');});
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.submit[name = 'welcomeLoginButton']"))).click();

I know there's no element named 'welcomeLoginButton' and only a class named .welcomeLoginButton. How can I get the form to submit itself after the username and password have been filled?


Solution

  • Just get the list of all the elements with the class welcomeLoginButton and click the 0th index.

    jQuery version:

    $('.welcomeLoginButton')[0].click();
    

    Vanilla JS versions:

    document.getElementsByClassName("welcomeLoginButton")[0].click();
    
    // querySelector without the All just returns the first index of the array
    document.querySelector('.welcomeLoginButton').click();
    
    document.querySelectorAll('.welcomeLoginButton')[0].click();