簡體   English   中英

Selenium C# 使用 IWebElement 的預期條件

[英]Selenium C# ExpectedConditions with IWebElement

我想用類似的方法制作一個 class WaitForElement 但是 ExpectedConditions.ElementIsVisible 期望(By)不是(IWebElement)

class WaitForElement
    {
(...)
public void waitUntilElementIsVisible(IWebElement element)
       {
           WebDriverWait webDriverWait = GetWebDriverWait();
           webDriverWait.Until ( ExpectedConditions.ElementIsVisible(element) );
       }

在我使用的 PageObject 類中

 public IWebElement orderTab => Driver.FindElement(By.XPath("//a[contains(@href,'order')]"));

所以我想這樣使用它:

WaitForElement.waitUntilElementIsVisible(orderTab)

有沒有辦法讓它工作? 我無法使用

public By orderTab => By.XPath("//a[contains(@href,'order')]");

等待元素可見的代碼實際上非常簡單。

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

wait.Until(d => d.FindElement(By.XPath("//a[contains(@href,'order')]")).Displayed);

我喜歡為 WebDriverWait class 創建擴展方法,如下所示:

namespace OpenQa.Selenium
{
    public static class WebDriverWaitExtensions
    {
        public static bool UntilElementIsDisplayed(this WebDriverWait wait, By locator)
        {
            return wait.Until(driver => driver.FindElement(locator).Displayed);
        }
    }
}

要使用它,您只需要一個 WebDriverWait object 並using OpenQa.Selenium; 在 C# 文件的頂部:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

wait.UntilElementIsVisible(By.XPath("//a[contains(@href,'order')]"));

暫無
暫無

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

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