收录日期:2019/03/20 20:01:30 时间:2009-07-29 18:38:48 标签:time,event-handling,selenium

I have a series of links on a page that take different times to load... I want to capture the amount of time each takes... The problem I am having is that the waitForPageToLoad time if exceeded causes the test to fail and the rest of my links do not get tested... I know I could just skip suspected links in the test or set the time limit way beyond expectation, but I was hoping there was an alternative to the waitForPageToLoad that could be used to trap when a page is loaded in Selenium, so that if a page takes longer than a minute to load it doesn't end the script.

Wawa's answer worked great for me, I just had to add the increment to the second var.

I also only wanted to wait 5 seconds, so I changed the break criteria.

int second = 0;
while(!selenium.IsElementPresent(mylink))
{
    if(second >= 5) 
        break;
    Thread.Sleep(1000);
    second++;
}

Here's what I would use:

int second = 0;
while(!selenium.IsElementPresent(mylink))
{
    if(second >= 300) 
        break;
    Thread.Sleep(1000);
}

You really don't need\want the try-catch (exception) block since IsElementPresent is not supposed to throw exceptions.

So it sounds like i might have to go to an export to perl or java to get the functionality i'm looking for then. But, the waitForElementPresent did help a lot. thanks :-)

something like...

    for (int second = 0; ; second++) {
        if (second >= 300) break;
        try {if (selenium.isElementPresent( mylink )) break;} 
        catch (Exception e) {}
        Thread.sleep(1000);
    }

The best way that you can do this is to set the timeout to a number far in the future because 30 seconds is the default timeout for selenium.

setTimout | 300

And then you can do waitForElementPresent call on an object on the next page if you don't want to do waitForPageToLoad