我有一个要解决的问题。 我使用Web驱动程序的wait方法来等待元素是可单击的,问题是,如果我在未连接到wait的wait中有错误,则不会显示该错误,并且我不知道是什么问题。 这是我在基本页面中构建的方法:
public void ClickElementWhenClickable(By locator) throws Exception {
WebDriver driver = getWebDriver();
driver.manage().timeouts().implicitlyWait(IMPLICIT_WAITE, SECONDS);
Instant now = Instant.now();
while (Instant.now().isBefore(now.plusSeconds(IMPLICIT_WAITE))) {
try {
new WebDriverWait(driver, IMPLICIT_WAITE).until(ExpectedConditions.elementToBeClickable(locator)).click();
return;
} catch (WebDriverException e) {
// do nothing, loop again
}
}
throw new Exception("Not able to click element <" + locator + "> within " + IMPLICIT_WAITE + "s.");
}
问题是,例如,如果我无法单击某个元素或某个元素不存在,则会收到我写的一般消息:
java.lang.AssertionError: Error in marking supply reporting s java.lang.Exception: Not able to click element <By.xpath: //span[contains(text(),'Detailed supply reporting')]> within 35s.
调查后,不使用wait方法,只需编写
driver.findElement(By.xpath("//label[@id='reporting']")).click();
例外是:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reporting" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
在我的方法中,它们是抛出最后一个等待的异常的一种方法,因为webdriver异常非常普遍,所以调查起来会容易得多