我有以下场景:
我想导航到一个页面。然后在按钮出现后立即单击它(不要等待页面加载)。我不想等待初始页面加载,因为这需要很长时间。我的程序目前卡住,直到页面加载然后单击按钮。
我基本上想导航到链接,然后无需等待页面并继续我的代码。
这附近有没有?
最佳答案
使用最新版本的 .NET 绑定(bind),您可以设置页面加载超时。但是,您需要注意一些注意事项。首先,并非所有浏览器都实现了这一点。如果没记错的话,它应该适用于 IE 和 Firefox。其次,您需要捕获异常才能使其正常工作,但这是可以做到的。
// WARNING! Completely untested code written without
// the benefit of an IDE!
IWebDriver driver = new InternetExplorerDriver();
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(1));
try
{
driver.Url = "http://your.long.loading.page.com";
}
catch (TimeoutException)
{
// NOTE: In 2.26 or later, this will be WebDriverTimeoutException
}
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id("yourId"));
}
关于c# - Selenium Webdriver c# 无需等待页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11922823/