为了检查元素是否存在并单击,我尝试编写一个布尔方法,该方法将等待使用C# selenium的webDriverWait启用和拆分元素,如下所示:
webDriverWait wait = new webDriverWait(driver, timeSpan.fromSeconds(60));
Wait.untill( d => webElement.enabled() && webElement.displayed());
如果上述情况没有发生,我希望方法返回'false‘。问题是我会被抛出异常。如果抛出noSuchElementException和timeOutException等异常,我如何忽略它们?我尝试过使用try catch块,但是它没有帮助,并且抛出了异常。
发布于 2017-03-29 19:56:56
WebDriverWait
实现了包含public void IgnoreExceptionTypes(params Type[] exceptionTypes)
方法的DefaultWait
类。
可以使用此方法定义在单击之前等待启用元素时要忽略的所有异常类型。
例如:
WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wdw.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
在前面的代码中,等待将忽略NoSuchElementException和ElementNotVisibleException异常
发布于 2017-03-29 13:37:40
如果您等待元素是可点击的,它也将被显示和启用。你可以简单的做
public bool IsElementClickable(By locator, int timeOut)
{
try
{
new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementToBeClickable(locator));
return true;
}
catch (WebDriverTimeoutException)
{
return false;
}
}
它将等待60年代,并在找到元素后单击它。如果没有找到元素,并且在超时过期后不能单击等等,它仍然会抛出异常。
编辑:将其封装在一个基于OPs注释的函数中。
https://stackoverflow.com/questions/43103129
复制