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



我想创建一个在测试套件中使用的通用方法。此方法应将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/.

提前谢谢。

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

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

WaitUntilElementVisible(按.Id("登录名"((;如果你想在这个调用中更改超时时间,你会这样调用: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));
}

最新更新