我有一个单击按钮的方法,但是,当它运行时,selenium会在成功点击时返回结果,而实际上,实际上没有点击按钮.如果我多次运行测试,偶尔会按预期点击它.我将我的测试框架设置为隐式等待大约15秒,我已经设置了明确的等待这个元素,但仍然看到相同的问题.当我执行< element> .isDisplayed()时,始终会找到该元素.我将.click放在一个while循环中,点击它几次,大部分时间都有效,有时候测试失败了.是否可以在单击按钮之前使用if语句检查元素是否实际显示?
我试过了:
- if(!element.isDisplayed){
- element.click
- }
这是我遇到问题的按钮:
- <button class="notkoButton listNew">
- <div class="l6e iconAdd">New List</div>
- </button>
这是我的方法:
- public marketing_lists_page navigateToNewListPage() throws Throwable {
- try {
- int x = 0;
- while(x < 5) {
- newListBtn.click();
- x++;
- }
- //newListPageHeader.isDisplayed();
- } catch (NoSuchElementException e){
- logs.errorDetails("Could not navigate to New List Page");
- Assert.fail();
- }
- return this;
- }
解决方法
看起来元素最初未启用或无法点击.要回答你的问题,是的,你可以使用明确的等待并等待元素可点击:
- WebDriverWait wait = new WebDriverWait(driver,timeOut);
- wait.until(ExpectedConditions.elementToBeClickable(locator));