Selenium WebDriver – 如何使用C#设置页面加载超时

我正在使用Selenium 2.20 WebDriver使用C#创建和管理firefox浏览器。 要访问页面,我使用以下代码,在访问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 

问题是,有时页面需要永远加载,并且看起来使用selenium 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))现在不推荐使用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#中的语法。 (不确定)

在ruby中它是

 @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

希望这可以帮助遇到此问题的其他人。

页面加载超时尚未在.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)); 

我们巴西人有一句蹩脚的解决方法“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)做了什么:

 ///  /// Waits until an element of the type  to show in the screen. ///  /// Element to be waited for. /// How long (in seconds) it should be waited for. ///  /// False: Never found the element. /// True: Element found. ///  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 ……

Make By你注意到页面中加载的内容(比如javascript元素和validation码)记住:它会在页面完全加载之前开始处理// rest of your application ,因此放置Thread.Sleep(1000)可能会很好) Thread.Sleep(1000)最后只是为了确定……

另请注意,此方法将在Selenium的DRIVER.Navigate().GoToUrl(url);的60秒标准超时后调用DRIVER.Navigate().GoToUrl(url);

不是最好的,但……正如我所说:一个好的冈比亚人完成工作……