[英]Selenium WebDriver - How to set Page Load Timeout using C#
I am using Selenium 2.20 WebDriver to create and manage a firefox browser with C#. 我正在使用Selenium 2.20 WebDriver用C#创建和管理firefox浏览器。 To visit a page, i use the following code, setting the driver timeouts before visiting the URL:
要访问页面,我使用以下代码,在访问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
The problem is that sometimes pages take forever to load, and it appears that the default timeout for a page to load using the selenium WebDriver is 30 seconds, which is too long. 问题在于,有时页面要花很长时间才能加载,并且似乎使用Selenium WebDriver加载页面的默认超时是30秒,这太长了。 And i don't believe the timeouts i am setting apply to the loading of a page using the GoToUrl() method.
而且我不相信我设置的超时适用于使用GoToUrl()方法加载页面。
So I am trying to figure out how to set a timeout for a page to load, however, i cannot find any property or method that actually works. 因此,我试图弄清楚如何设置要加载的页面的超时时间,但是,我找不到实际起作用的任何属性或方法。 The default 30 second timeout also seems to apply to when i click an element.
默认的30秒超时似乎也适用于单击元素时。
Is there a way to set the page load timeout to a specific value so that when i call the GoToUrl() method it will only wait my specified time before continuing? 有没有一种方法可以将页面加载超时设置为特定值,以便在我调用GoToUrl()方法时仅等待指定的时间后才能继续?
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);
注意:现在不推荐使用driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5))
。
如果这可以帮助仍然在寻找答案的任何人,则C#WebDriver API现在包含适当的方法。
driver.Manage().Timeouts().SetPageLoadTimeout(timespan)
With this you should be able to declare a wait explicitly. 这样,您应该可以显式声明一个等待。
WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(time in seconds));
wait.until(Your condition)
you could also change the implicit wait time 您还可以更改隐式等待时间
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
I think that is the syntax in C#. 我认为这是C#中的语法。 (not to sure)
(不确定)
In ruby it is 在红宝石中
@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 30)
i found the solution this this issue. 我发现这个问题的解决方案。 When creating a new FirefoxDriver, there are overloads in the constructor that allow you to specify a command timeout which is the maximum time to wait for each command, and it seems to be working when calling the GoToUrl() method:
创建新的FirefoxDriver时,构造函数中存在重载,可让您指定命令超时,该超时是等待每个命令的最长时间,并且在调用GoToUrl()方法时似乎可以正常工作:
driver = new FirefoxDriver(new FirefoxBinary(), profile, new TimeSpan(0, 0, 0, timeoutSeconds));
link to FirefoxDriver constructor documentation for reference: http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_OpenQA_Selenium_Firefox_FirefoxDriver__ctor_2.htm 链接到FirefoxDriver构造函数文档以供参考: http ://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_OpenQA_Selenium_Firefox_FirefoxDriver__ctor_2.htm
Hope this helps someone else who runs into this problem. 希望这可以帮助遇到此问题的其他人。
As of 2018: Besides these: 截至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));
wait for searching for an item, loading a page, and waiting for script respectively. 等待搜索项目,加载页面和等待脚本。 There is:
有:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
Page load timeouts are not implemented in the .NET bindings yet. .NET绑定中尚未实现页面加载超时。 Hopefully they will be soon.
希望他们会很快。
We brazilians have a word for crappy workarounds "Gambiarra"... Well... at least they do the job... Here is mine: 我们巴西人对“ 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
What my WaitElement(By, int)
does: 我的
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; }
}
After this, you can play with timeout
... 在此之后,您可以玩
timeout
...
Make By
the things you notice that loads last in the page (like javascript elements and captchas) remembering: it will start working the // rest of your application
before the page fully loads, therefore may be nice to put a Thread.Sleep(1000)
at the end just to be sure... By
您注意到加载在页面中持续进行的事情(例如javascript元素和验证码),记住:它将在页面完全加载之前开始// rest of your application
,因此放置Thread.Sleep(1000)
最后只是为了确保...
Also notice that this method will be called AFTER the 60 seconds standard timeout from Selenium's DRIVER.Navigate().GoToUrl(url);
还要注意,在Selenium的
DRIVER.Navigate().GoToUrl(url);
的60秒标准超时之后,将调用此方法DRIVER.Navigate().GoToUrl(url);
Not the best, but... as I said: A good gambiarra gets the job done... 并不是最好的,但是...就像我说的:一个好的冈比亚人可以完成工作...
For anyone who wants the opposite effect: setting timeout longer than 60s. 对于任何想要相反效果的人:将超时设置为超过60s。
You need both use: 您需要同时使用:
new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), new FirefoxOptions(), TimeSpan.FromSeconds(120))
and 和
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);
The new FirefoxDriver(binary, profile, timeSpan)
has been obsolete. new FirefoxDriver(binary, profile, timeSpan)
已过时。
driver.Manage().Timeouts().SetPageLoadTimeout(timespan)
does not work. 不起作用。
This works. 这可行。 Use a property setter syntax.
使用属性设置器语法。
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.