如何强制Selenium继续下一个进程,而无需等待完成页面加载
本文关键字:等待 加载 Selenium 何强制 继续 下一个 进程 | 更新日期: 2023-09-27 18:03:04
通常,在导航到url后,Selenium WebDriver等待完成页面以继续下一个进程(下一行代码)。如果这个过程需要很长时间,那么它将抛出超时异常。我不想等太久。在10秒后导航到一个url,我不想等到它完成。
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
}
我想继续获取元素,即使页面没有完全加载
您可能希望将页面加载超时设置为所需的秒数。
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
}
}