0

我想创建一个在整个测试套件中使用的通用方法。此方法应将 web 元素作为参数。然后该方法应该返回truefalse基于它在配置的超时期间的可见性。

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/中查看我的进度。

提前致谢。

4

1 回答 1

0

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

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

WaitUntilElementVisible(By.Id("登录")); 如果您想在此调用中更改超时时间,您将调用如下:WaitUntilElementVisible(By.Id("Login"),45);

你可以把你的 Try and catch 逻辑放在那里。我使用 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));

        }
于 2020-03-27T07:55:07.070 回答