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

本文关键字:加载 超时 设置 WebDriver 何使用 Selenium | 更新日期: 2023-09-27 18:34:19

我正在使用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() 方法时,它只会等待我指定的时间才能继续?

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

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);

注意:driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5))现已弃用。

如果这可以帮助任何仍在寻找答案的人,C# WebDriver API 现在包含适当的方法。

driver.Manage().Timeouts().SetPageLoadTimeout(timespan)

有了这个,你应该能够明确地声明等待。

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));

链接到 Firefox Driver 构造函数文档以供参考:http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_OpenQA_Selenium_Firefox_FirefoxDriver__ctor_2.htm

希望这有助于遇到此问题的其他人。

我们巴西人有一个蹩脚的变通词"冈比亚拉"......井。。。至少他们完成了这项工作...这是我的:

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)只是为了确保......

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

不是最好的,但是...正如我所说:一个好的冈比亚拉可以完成工作......

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

对于任何想要相反效果的人:设置超过 60 秒的超时。

您需要同时使用:

new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), new FirefoxOptions(), TimeSpan.FromSeconds(120))

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);

new FirefoxDriver(binary, profile, timeSpan)已经过时。

driver.Manage().Timeouts().SetPageLoadTimeout(timespan) 

不起作用。

这行得通。使用属性资源库语法。

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);