等待显示元素的方法,但是需要将元素作为参数传递,以使其成为要使用的通用方法

问题描述 投票:0回答:1

我想创建在整个测试套件中使用的通用方法。此方法应将web元素作为参数。然后,该方法应根据配置的超时期间的可见性返回true或false。

    public bool waitForElementVisibility(IWebDriver driver, IWebElement element)
        {
          try
          {
           //code to wait until element is displayed similar to this. But instead of passing By.Id(login) pass the element
           new WebDriverWait(driver,TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((By.Id(login))));
           return true;
          }
          catch(Exception e)
          {
           return false;
          }
        }

我正在用C#创建硒框架。您可以在此https://umangamadawala.blogspot.com/中看到我的进度。

提前感谢。

c# selenium-webdriver webdriverwait
1个回答
0
投票

我认为您正在搜索类似以下代码的内容:

您可以毫无问题地将By.Id(“ Login”)作为By元素传递,因此对该函数的调用将类似于:

WaitUntilElementVisible(By.Id(“ Login”));如果要更改此调用的超时时间,则将这样调用:WaitUntilElementVisible(By.Id(“ Login”),45);

您可以将“尝试并捕获”逻辑放在这里。我使用ElementIsVIsible,但您只能使用ElementExist更改呼叫。

protected virtual IWebElement WaitUntilElementVisible(By elementLocator, int timeout = 30)
        {
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));

                return wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(elementLocator));

        }
© www.soinside.com 2019 - 2024. All rights reserved.