C# Selenium WebDriver-如何使用C设置页面加载超时#
我正在使用Selenium 2.20 WebDriver创建和管理带有C#的firefox浏览器。要访问页面,我使用以下代码,在访问URL之前设置驱动程序超时:C# Selenium WebDriver-如何使用C设置页面加载超时#,c#,selenium,timeout,webdriver,wait,C#,Selenium,Timeout,Webdriver,Wait,我正在使用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
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
问题是,有时页面加载会花费很长时间,而且使用SeleniumWebDriver加载页面的默认超时时间似乎是30秒,这太长了。我不相信我设置的超时适用于使用gotour()方法加载页面
因此,我试图找出如何设置页面加载的超时时间,但是,我找不到任何实际工作的属性或方法。默认的30秒超时似乎也适用于单击元素时
是否有方法将页面加载超时设置为特定值,以便在调用gotour()方法时,它只等待指定的时间,然后继续?通过此方法,您应该能够显式声明等待
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)
页加载超时尚未在.NET绑定中实现。希望他们很快就会来。我找到了解决这个问题的办法。创建新的FirefoxDriver时,构造函数中存在重载,允许您指定命令超时,这是等待每个命令的最长时间,并且在调用gotour()方法时,它似乎起作用: 链接到FirefoxDriver构造函数文档以供参考:
希望这能帮助遇到此问题的其他人。如果这能帮助仍在寻找答案的人,C#WebDriver API现在包含了相应的方法
driver.Manage().Timeouts().SetPageLoadTimeout(timespan)
注意:driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5))
现在已被弃用。自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));
我们巴西人有一个词来形容糟糕的变通办法“冈比亚”。。。好。。。至少他们完成了任务。。。 这是我的:
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
myWaitElement(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; }
}
//
///等待该类型的元素显示在屏幕中。
///
///要等待的元素。
///应该等待多长时间(以秒为单位)。
///
///False:从未找到该元素。
///True:找到元素。
///
私有bool WaitElement(按元素,int超时)
{
试一试{
WriteLine($“-正在等待元素{element.ToString()}”);
int timesToWait=timeout*4;//等待1/4秒的时间。
int waitedTimes=0;//等待的次数。
//此设置在7秒时超时。您可以更改代码以通过
做{
等待时间++;
如果(等待时间>=等待时间){
Console.WriteLine($”--在()中找不到元素+
$“{(timesToWait*0.25)}秒。取消节…”);
返回false;
}
睡眠(250);
}而(!ExistsElement(element));
Console.WriteLine($“--找到元素。正在继续…”);
//Thread.Sleep(1000);//可能适用于此处
返回true;
}接住{throw;}
}
之后,您可以玩超时
请记住:在页面完全加载之前,应用程序的其余部分将开始工作,因此最好在最后添加一个线程。Sleep(1000)
,以确保
还请注意,此方法将在Selenium的DRIVER.Navigate().gotour(url)的60秒标准超时后调用代码>
不是最好的,但是。。。正如我所说:一个好的冈比亚人能完成任务
driver.Manage().Timeouts().SetPageLoadTimeout(timespan)
不起作用
这很有效。使用属性设置器语法
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);
对于任何想要相反效果的人:设置超时时间超过60秒
您需要同时使用:
new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), new FirefoxOptions(), TimeSpan.FromSeconds(120))
及
新的FirefoxDriver(二进制、配置文件、时间跨度)
已过时。您确定gotour()
等待页面加载吗?我的经验是,它不是。但这只是一种感觉,不是事实。是的,我100%确信调用gotour()会阻止执行,直到页面完全加载完毕,我测量了调用此方法的默认超时30秒,30秒后执行将继续,我试图以某种方式减少默认超时30秒。我发布了类似的问题:@TorbjörnKalin它确实会阻塞,直到页面加载到浏览器抛出onReady事件为止。如果有后js和ajax。。。“页面加载”knowlege并不意味着什么。这将不起作用,因为执行被gotour()方法阻止,所以在该方法完成或超时之前,我无法执行任何代码,默认值为30秒。感谢您的回复。因此您需要更改隐式等待时间-driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5))代码>将5秒减少到您想要的更少。这将影响您在gotourl上的等待时间。您还可以尝试wait.until(driver.Navigate().gotourl(myUrl));//转到页面url
,其中您可以显式定义等待执行gotoURL命令的时间executed@seleniumnewbie:我不知道为什么对wait.until(driver.Navigate().gotour(myUrl));//转到页面url,但从我得到的结果来看,这根本不起作用。我发布了类似的问题:我只希望有人能清楚地描述页面加载超时与隐式等待的区别。页面何时加载而元素未加载?从C#intellisense隐式Wait
是在页面上搜索元素的时间量,SetPageLoadTimeout
是等待URL加载的时间量,以及<
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);
new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), new FirefoxOptions(), TimeSpan.FromSeconds(120))
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);