我使用的硒2.20 webdriver的创建和管理一个Firefox浏览器使用C#。 要访问一个网页,我使用下面的代码,访问URL之前设置的驱动程序超时:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); // Set implicit wait timeouts to 5 secs
driver.Manage().Timeouts().SetScriptTimeout(new TimeSpan(0, 0, 0, 5)); // Set script timeouts to 5 secs
driver.Navigate().GoToUrl(myUrl); // Goto page url
问题是,有时网页需要永远载入,它似乎是一个页面的默认超时使用硒的webdriver是30秒,这是太长加载。 而且我不相信我是设置超时适用于使用GoToUrl()方法的网页的加载速度。
所以我想弄清楚如何设置超时网页的加载,但是,我无法找到实际工作的任何属性或方法。 该默认值为30秒超时也似乎适用于当我点击的元素。
有没有办法在页面加载超时设置为一个特定的值,这样,当我打电话GoToUrl()方法,它不仅会继续等待我指定的时间?
在这种情况下,任何人都可以帮助还在寻找这个问题的答案,在C#webdriver的API现在包含适当的方法。
driver.Manage().Timeouts().SetPageLoadTimeout(timespan)
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);
注: driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5))
现在已经过时。
有了这个,你应该能够显式声明的等待。
WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(time in seconds));
wait.until(Your condition)
你也可以改变隐含的等待时间
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
我认为这是在C#的语法。 (不知道)
在红宝石是
@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 30)
我找到了解决这个这个问题。 当创建一个新FirefoxDriver,也有在构造函数允许你指定一个命令超时是等待每个命令的最长时间过载,似乎调用GoToUrl()方法时,被工作:
driver = new FirefoxDriver(new FirefoxBinary(), profile, new TimeSpan(0, 0, 0, timeoutSeconds));
链接FirefoxDriver构造文档以供参考: http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_OpenQA_Selenium_Firefox_FirefoxDriver__ctor_2.htm
希望这可以帮助别人谁运行到这个问题。
我们巴西人有蹩脚的解决方法“Gambiarra”一句话嘛......至少他们做的工作......这是我的:
var url = "www.your.url.here"
try {
DRIVER.Navigate().GoToUrl(url);
} catch {
// Here you can freely use the Selenium's By class:
WaitElement(By.Id("element_id_or_class_or_whatever_to_be_waited"), 60);
}
// rest of your application
什么我WaitElement(By, int)
的作用:
/// <summary>
/// Waits until an element of the type <paramref name="element"/> to show in the screen.
/// </summary>
/// <param name="element">Element to be waited for.</param>
/// <param name="timeout">How long (in seconds) it should be waited for.</param>
/// <returns>
/// False: Never found the element.
/// True: Element found.
/// </returns>
private bool WaitElement(By element, int timeout)
{
try {
Console.WriteLine($" - Waiting for the element {element.ToString()}");
int timesToWait = timeout * 4; // Times to wait for 1/4 of a second.
int waitedTimes = 0; // Times waited.
// This setup timesout at 7 seconds. you can change the code to pass the
do {
waitedTimes++;
if (waitedTimes >= timesToWait) {
Console.WriteLine($" -- Element not found within (" +
$"{(timesToWait * 0.25)} seconds). Canceling section...");
return false;
}
Thread.Sleep(250);
} while (!ExistsElement(element));
Console.WriteLine($" -- Element found. Continuing...");
// Thread.Sleep(1000); // may apply here
return true;
} catch { throw; }
}
在此之后,你可以玩timeout
...
请By
您注意到负载最后在页面(如JavaScript元素和验证码),记住的东西:它会开始工作// rest of your application
的页面完全加载之前,因此可能是不错的放Thread.Sleep(1000)
在刚刚结束是的......
另请注意,这个方法会被称为硒的60秒的标准超时后DRIVER.Navigate().GoToUrl(url);
不是最好的,但是......我说:一个好的gambiarra干得不错...
页面加载超时未在.NET绑定来实现呢。 希望他们会很快。
截至2018:除了这些:
driver.Manage().Timeouts().ImplicitWait.Add(System.TimeSpan.FromSeconds(5));
driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(5));
driver.Manage().Timeouts().AsynchronousJavaScript.Add(timespan));
等待搜索项,加载页面,并分别等待脚本。 有:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
driver.Manage().Timeouts().SetPageLoadTimeout(timespan)
不工作。
这工作。 使用属性setter语法。
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);