簡體   English   中英

Selenium Webdriver C#-如何消除這種辛苦的等待?

[英]Selenium Webdriver C# - how can I remove this hard wait?

對於以下問題,我很難過。 我正在嘗試瀏覽一些網頁,這些網頁具有各種輸入(文本框/下拉列表/按鈕),其次是底部的繼續按鈕以移至下一個屏幕。 我的測試經常失敗,因為它們不能始終定位第一個元素以便與之交互。

據我所知,該頁面沒有執行任何精美的AJAX后期加載或其他任何操作,因此,一旦頁面加載完畢,Webdriver應該能夠找到每個元素。

我的代碼:

框架

public static void WaitOnPageForXPathClickable(string selector)
    {
        new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable((By.XPath(selector))));
    }

測試用例

Utilities.WaitOnPageForXPathClickable("the xpath of the continue button and checks that it is clickable");
Driver.Instance.FindElement(By.XPath("the xpath of the first button that I want to click")).Click();

我是否需要嘗試並包含一個函數以確保在執行測試之前已完全加載頁面? 我對此感到困惑,因為我已經閱讀到Selenium已經在默認情況下等待頁面加載。 我本以為,等待一個元素(“繼續”按鈕)被單擊應該意味着所有其他元素也都准備就緒了嗎? 我真的想避免在單擊每個元素之前必須等待每個元素。

waiting繼續”按鈕Utilities.WaitOnPageForXPathClickable("the xpath of the continue button and checks that it is clickable");使事情變得更加復雜Utilities.WaitOnPageForXPathClickable("the xpath of the continue button and checks that it is clickable"); 繼續按鈕Utilities.WaitOnPageForXPathClickable("the xpath of the continue button and checks that it is clickable"); 在下一步中嘗試在“ 第一個按鈕Driver.Instance.FindElement(By.XPath("the xpath of the first button that I want to click")).Click();click() Driver.Instance.FindElement(By.XPath("the xpath of the first button that I want to click")).Click();

測試場景:

理想情況下,您的Test Scenario應為:

測試用例:

Utilities.WaitOnPageForXPathClickable("the xpath of the first button that I want to click");
Driver.Instance.FindElement(By.XPath("the xpath of the first button that I want to click")).Click();

框架:

public static void WaitOnPageForXPathClickable(string selector)
{
    IWebElement element = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable((By.XPath(//label[@class='replaced-input-label replaced-input-label--radio']))));
    elementClick();
}

您能分享html嗎,您是否嘗試過使用Visible而不是Clickable

public static void WaitOnPageForXPathClickable(string selector)
{
    new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible((By.XPath(selector))));
}

您可以等待頁面完全加載,例如:

        public void WaitForPageToBeFullyLoaded(int timeSpan)
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(timeSpan)).Until(
            d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
    }

您也可以檢查顯示或已啟用元素。

暫無
暫無

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

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