通常,在分配到网址后,Selenium WebDriver会等待完成页面以继续下一个进程(下一行代码)。如果进程需要很长时间,那么它将抛出超时异常。我不想等太久。在导航到网址10秒后,我不想等到它完成。
using (IWebDriver driver = new FirefoxDriver())
{
driver.Navigate().GoToUrl("www.mywebsite.com"); //takes a long time here. More than 40 seconds to complete whole page
//here I don't want to wait too long
// <input type="text" id="tp-test-selenium" />
var element = driver.FindElement(By.Id("tp-test-selenium")); //I should be able to access this text input without waiting 40 seconds
}
即使页面未完全加载,我也想继续获取元素。
答案 0 :(得分:0)
您可能希望将页面加载超时设置为所需的秒数。
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));
然后可能用try / catch包装GotoUrl()
using (IWebDriver driver = new FirefoxDriver())
{
try
{
driver.Navigate().GoToUrl("www.mywebsite.com"); //takes a long time here. More than 40 seconds to complete whole page
}
catch(WebPageTimeoutException e)
{
//Log it or not
}
finally
{
// It will perform your steps either if page loaded correctly during timeout set or after timeout expired
//here I don't want to wait too long
// <input type="text" id="tp-test-selenium" />
var element = driver.FindElement(By.Id("tp-test-selenium")); //I should be able to access this text input without waiting 40 seconds
}
}