alternative for waitForPageToLoad in Selenium

偶尔善良 提交于 2019-12-06 11:59:56

问题


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.


回答1:


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++;
}



回答2:


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.




回答3:


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);
    }



回答4:


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



来源:https://stackoverflow.com/questions/1202218/alternative-for-waitforpagetoload-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!