簡體   English   中英

Selenium WebDriver-如何使用C#設置頁面加載超時

[英]Selenium WebDriver - How to set Page Load Timeout using 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()方法時僅等待指定的時間后才能繼續?

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

鏈接到FirefoxDriver構造函數文檔以供參考: http ://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_OpenQA_Selenium_Firefox_FirefoxDriver__ctor_2.htm

希望這可以幫助遇到此問題的其他人。

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

.NET綁定中尚未實現頁面加載超時。 希望他們會很快。

我們巴西人對“ 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)最后只是為了確保...

還要注意,在Selenium的DRIVER.Navigate().GoToUrl(url);的60秒標准超時之后,將調用此方法DRIVER.Navigate().GoToUrl(url);

並不是最好的,但是...就像我說的:一個好的岡比亞人可以完成工作...

對於任何想要相反效果的人:將超時設置為超過60s。

您需要同時使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM