user2251126
user2251126

Reputation: 25

document.getElementById does not return null, but also does not do what I want. No error in javascript console

In this instance, I load a single paypal page, in which I am prompted to login. Once I login, the page changes, through the use of other javascripts on paypal's end. The address does not change on this transition, nor does the source code in any material way. I am trying to find a way to have my script wait long enough after the first click to be able to get the element that loads after. I thought I could do this fairly simple using the following:

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

window.onload = function() {
    document.getElementById("continue").click();
}; 

When the script is executed, the first button is clicked, the page transitions, but it won't click the second button that loads. My javascript console does not report any errors, suggesting that it is able to "get" the element. Not sure why it won't click it though.

Upvotes: 0

Views: 660

Answers (1)

Dagg Nabbit
Dagg Nabbit

Reputation: 76736

If nothing else, you could always poll for the existence of the "continue" element at some interval:

function clickContinue() {
    var button = document.getElementById("continue");
    return button ? button.click() : setTimeout(clickContinue, 100);
}

document.getElementById("submitLogin").click();
clickContinue();

If you go this route, you'll probably want to include a failsafe so it doesn't run too long, in case something unexpected happens. Something like this should work:

clickContinue.interval = 100; // Look for "continue" button every 0.1 second
clickContinue.ttl = 10000; // Approximate time to live: 10 seconds ~ 10,000 ms
clickContinue.tries = clickContinue.ttl / clickContinue.interval | 0;

function clickContinue() {
    var button = document.getElementById("continue"),
        interval = clickContinue.interval;
    return button ? button.click() : 
        clickContinue.tries-- && setTimeout(clickContinue, interval);
}
// ...

Take a look at PayPal's API docs and see if they provide a way to set up a callback to handle this, though. This polling technique should probably only be used as a last resort.

Upvotes: 2

Related Questions