java - 按钮不可见并返回 TRUE,按钮 xpath 在网页中,用于 selenium webdriver
问题描述
使用 selenium webdriver 测试按钮是否不可见,布尔值始终返回 true。
使用下面的代码并返回 TRUE,在网页中我可以看到 Add 按钮的 xpath。按钮不可见。
boolean present;
try {
//driver.findElement(By.xpath("//button[@id='add']"));
driver.findElement(By.xpath("//button[@id='add']")).isDisplayed();
present = true;
System.out.println("Add button is present");
} catch (NoSuchElementException e) {
present = false;
System.out.println("Add button is not present:" +e.getMessage());
}
xpath: addButtonxpath
解决方案
这是代码片段
boolean present;
try {
present = driver.findElement(By.xpath("//button[@id='add']")).isDisplayed();
System.out.println("Add button is present");
} catch (NoSuchElementException e) {
present = false;
System.out.println("Add button is not present:" + e.getMessage());
} catch (Exception e) {
present = false;
System.out.println("Add button is not present:" + e.getMessage());
}
推荐阅读
- sql - 如何将所有列名放入一列?
- amazon-web-services - AWS elasticache redis集群在禁用集群模式时在线调整大小
- c# - 如何捕获防伪异常并重定向到错误页面
- html - 为什么我的页面元素不尊重我的本地 Site.css 文件?
- css - 导航栏 - 子元素不继承宽度和重叠
- python - 我的 django web 应用程序保留了内存中的旧图像数据。每次提交后如何清除?
- java - MultiLayerNetwork 的所有方法上的“无法解析符号”
- javascript - 如何从另一个 Vue 组件触发 Mapbox 事件?
- mrtk - MRTK (V2.5) 总是无法使用构建窗口部署到 Hololens 2
- algorithm - Number of unique elements only using equality comparisons