我有一个自定义等待方法,定义为:
public IWebElement WaitForElementClickable(IWebDriver _driver, By elementName)
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(20));
return wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(elementName))
}我有一个地方,我点击一个按钮和一个新的页面加载,它坚持几秒钟“加载”(2-3秒),然后我想点击其他东西,一旦它加载.
public void enterSearchInfo()
{
//Thread.Sleep(2000);
IWebElement selectElement = utility.WaitForElementClickable(_driver, element);
selectElement.Click();
}即使我将等待方法设置为20秒
OpenQA.Selenium.ElementClickInterceptedException: element click intercepted:当我取消评论Thread.Sleep(2000)时,它会工作10 / 10次
有比wait for element clickable方法更好的方法来处理这个问题吗?我不想在代码中硬编码睡眠等待。
发布于 2021-03-26 16:25:24
这是我在我创建的框架中使用的。它吃掉ElementClickInterceptedException和StaleElementReferenceException,并一直尝试直到超时或成功。它解决了很多问题,比如你在说什么。对于每个页面,有其他方法可以实现它,但是我发现在很多情况下,这种方法非常有效。
/// <summary>
/// Clicks on an element
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <param name="timeOut">[Optional] How long to wait for the element (in seconds). The default timeOut is 10s.</param>
public void Click(By locator, int timeOut = 10)
{
DateTime now = DateTime.Now;
while (DateTime.Now < now.AddSeconds(timeOut))
{
try
{
new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementToBeClickable(locator)).Click();
return;
}
catch (ElementClickInterceptedException)
{
// do nothing, loop again
}
catch (StaleElementReferenceException)
{
// do nothing, loop again
}
}
throw new Exception($"Unable to click element <{locator}> within {timeOut}s.");
}https://stackoverflow.com/questions/66820416
复制相似问题