java - Not able to click "Close" button of a Modal In Selenium Webdriver (Java)
问题描述
After clicking on a link a modal appear where a close button exist. I tried to close the modal with below code but it's not working.
WebElement element = driver1.findElement(By.className("btn btn-secondary"));
if (element.isEnabled()) {
element.click();
} else {
System.out.println("Disable");
}
解决方案
我可以从您的定位器中得知您至少遇到了错误,
不允许使用复合类名称
By.className()
需要一个类名,但您提供了两个将导致上述错误。如果没有 HTML,很难说最好的定位器可能是什么,但可能有用的是 CSS 选择器,
By.cssSelector(".btn.btn-secondary")
尽管这是通用的,但匹配的元素可能不止一个。
您可能需要添加等待,特别是因为您正在处理模式对话框,
new WebDriverWait(driver).until(ExpectedConditions.elementToBeClickable(...)).click();
您可能需要再次等待以确保在脚本继续之前对话框关闭。
推荐阅读
- micronaut - 使用 micronaut 访问记录器记录用户
- amazon-web-services - 在widevine+dash DRM视频加密中,分段mp4上没有设置签名cookie
- python - 如果我添加颜色,则 plotly express 重新排序分类 x 轴散点图
- javascript - React native App 在 IOS 模拟器上不起作用
- python - 为什么 pandas 的 replace 方法不适用于 None 作为值?
- javascript - React native navigation.setParams 没有设置任何新参数
- address-sanitizer - 您可以在 ASAN sanitizer 之前运行用户定义的通行证吗?
- reporting-services - 如何将报表的一部分对齐到最左端,而报表的其余内容在 SSRS 中放置得不太远?
- elasticsearch - 为什么 ElasticSearch function_score 权重没有按预期工作?
- java - ModelMapper 将 List<...> 映射到对象属性