umbraphile
umbraphile

Reputation: 377

selenium.waitForPageToLoad doesn't seem to be waiting

I'm updating a Selenium program I wrote a while back and part of it has stopped working. I want to go through a whole series of links on a page, click on each, making sure that some expected text is present. But sometimes a log-in page (https://library.med.nyu.edu/sso/ezproxy_form.php) appears before the desired page, in which case I need to log in before checking the page. The problem is, no matter what string I put in to check whether I've landed on the log in page, Selenium concludes it's not present and skips logging in, obviously causing everything else to fail. See below--I'm not sure that was actually the problem. It seems to be instead that it's rushing through the "if we need to sign in" code without actually signing in, then obviously failing the main part of the test because it's not on the right page.

Here's the code--does anyone see my mistake?

       for (int i = 0; i < Resources.size(); i++) {
        try {
                        selenium.open("/");
                        selenium.click("link=" + Resources.get(i).link);
                        selenium.waitForPageToLoad("100000");
                        if (selenium.isTextPresent("Please sign in to access NYUHSL e-resources")) {
                            selenium.type("sso_user", kid);
                            selenium.type("sso_pass", password);
                            selenium.click("name=SignIn");
                            selenium.waitForPageToLoad("100000");
                        }

                        if (!selenium.isTextPresent(Resources.get(i).text)) {
                            outfile.println(Resources.get(i).name + " failed");
                        }
                    } catch (Exception e) {
                        outfile.println(Resources.get(i).name + " could not be found--link removed?");
                    }
       }

Upvotes: 1

Views: 4144

Answers (2)

Freddy Vega
Freddy Vega

Reputation: 126

Try putting:

selenium.setSpeed("1000");

Right after the selenium.open this will inject 1 second delay (1000ms) between selenium commands. You should make it a standard practice to add this, especially if you're not running headless browsers.

Also you might consider using, since you know the url you are expecting to be on if on the login page, the selenium command getLocation. This will return the absolute URL of the current page. Might be more effective than trying to look for elements that can change at any time within the page.

So to use getLocation in your code above:

if (selenium.getLocation() == "your reference url"){
do your login stuff here
}

Again this is just a sample to illustrate what I'm saying. Hope it helps you out.

Upvotes: 1

user766038
user766038

Reputation:

Does the login page have a page title? If yes, try validating the page title using selenium.getTitle() method to check if you are headed to login page. If not, proceed clicking on the link without logging in.

I think page title validation can help resolve this issue

Upvotes: 1

Related Questions