Script to automate the passing credentials to next page

I have one URL which asks me to " click here to login" then the next page loads. once the next page loads I need to pass the credential. tried the below script it clicks on the button but doesn’t pass the credentials on second page

the process should be: in first-page script is clicking on next button
in second page script is passing the credentials .

document.getElementsByClassName('click_hereTo_login')[0].click();

$( document ).ready(function() 
{ if document.location.href=="https://example.com/secondpage"
then {
  document.getElementById('id').value="12322";
document.getElementById('password').value="4444";
document.getElementsByClassName('login')[0].click();
}})