java - 如何通过 Selenium 和 Java 根据 html 单击取消按钮
问题描述
HTML:
<button type="button" class="modal-footer-button g-capitalize btn btn link">Cancel</button>
代码试用:
By.xpath("//button[@type='button']").click()
没用。
也尝试了许多其他方法。无法单击取消按钮。错误:
org.openqa.selenium.NoSuchElementException: 没有这样的元素: 无法定位元素: {"method":"xpath","selector":"//button[@type='button'][@class='modal-footer -button g-capitalize btn btn-link'][@value='Cancel']"}
解决方案
这可能是时间问题。尝试等到所需的按钮出现在 DOM 中:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='fade modal' and @role='dialog']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class, 'modal-footer-button') and text()='Cancel']"))).click();
推荐阅读
- javascript - 如何根据另一个参数更改函数参数的数量?
- oauth - 如何使用适用于 Web 服务器应用程序的 Google OAuth 2.0 向 Google ID_Token 添加自定义声明
- nuget-package - 排除 NuSpec 文件中除缩小版本外的文件
- android - 颤动中的眨眼检测?
- javascript - 从子div获取父div
- php - 如何动态扩展 Angular 网站?
- jestjs - 在开玩笑测试中输入值时,form.item 似乎没有获得经过验证的类
- nestjs - Nestjs中的字节范围请求
- python - Dash plotly data table Fixed column with merge_duplicate_headers=True 导致滚动条滚动到表格宽度之外
- python - 单元测试以捕获不正确的实现