简体   繁体   English

Selenium C# 使用 IWebElement 的预期条件

[英]Selenium C# ExpectedConditions with IWebElement

I want to make a class WaitForElement with methods likes But ExpectedConditions.ElementIsVisible expect (By) not (IWebElement)我想用类似的方法制作一个 class WaitForElement 但是 ExpectedConditions.ElementIsVisible 期望(By)不是(IWebElement)

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

In my PageObject classes I use在我使用的 PageObject 类中

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

so I wanted to use it like that:所以我想这样使用它:

WaitForElement.waitUntilElementIsVisible(orderTab)

Is there any option to make it works?有没有办法让它工作? I can't use我无法使用

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

The code to wait for an element to be visible is actually very simple.等待元素可见的代码实际上非常简单。

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

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

I like to create extension methods for the WebDriverWait class for things like this:我喜欢为 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);
        }
    }
}

And to use it, all you need is a WebDriverWait object and using OpenQa.Selenium;要使用它,您只需要一个 WebDriverWait object 并using OpenQa.Selenium; at the top of the C# file:在 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