selenium-webdriver - 页面上的按钮列表。获得列表后,我想检查按钮是否显示在页面上
问题描述
我是自动化测试的新手,我整天都在努力解决这个问题。无论如何,我想去该网站并获取页面上的按钮列表。比我想打印页面上显示的按钮列表。
package com.practice;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Buttons {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Oderint dum metuant\\eclipse-workspace\\JAR FILES\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.toolsqa.com/automation-practice-switch-windows/");
List <WebElement> buttons = driver.findElements(By.tagName("button"));
for ( int i=0; i<buttons.size();i++){
WebElement button = buttons.get(i);
if(button.isEnabled()){
System.out.println(buttons);
}
}
}
}
这是我运行代码时得到的:
[[ChromeDriver: chrome on XP (fddbb691263a84a531368a18e6d495b3)] -> 标签名称: button] [[ChromeDriver: chrome on XP (fddbb691263a84a531368a18e6d495b3)] -> 标签名称: button] [[ChromeDriver: chrome on XP (fddbb691263a84a531e tag6d495b3)名称:按钮] [[ChromeDriver:XP 上的 chrome (fddbb691263a84a531368a18e6d495b3)] -> 标签名称:按钮] [[ChromeDriver:XP 上的 chrome (fddbb691263a84a531368a18e6d495b3)] -> 标签名称:按钮] [[ChromeDriver:XP 上的 chrome (fddbb691a263e648) ] -> 标签名称:按钮] [[ChromeDriver: chrome on XP (fddbb691263a84a531368a18e6d495b3)] -> 标签名称:button] [[ChromeDriver: chrome on XP (fddbb691263a84a531368a18e6d495b3)] -> 标签名称:button] [[ChromeDriver: chrome on XP (fddbb691263a84a531368a18e6d495b3)] -> 标签名称:按钮] [[ChromeDriver:XP上的chrome(fddbb691263a84a531368a18e6d495b3)]->标签名称:按钮] [[ChromeDriver:XP上的chrome(fddbb691263a84a531368a18e6d495b3)]->标签名称:按钮] [[ChromeDriver:XP上的chrome(fddbb691263a864a5313368a1)标签名称:495bd] [[ChromeDriver: chrome on XP (fddbb691263a84a531368a18e6d495b3)] -> 标签名称: button] [[ChromeDriver: chrome on XP (fddbb691263a84a531368a18e6d495b3)] -> 标签名称: button]
解决方案
基本上有三件事
Thread.sleep(10000); // you can also use web driver's implicit and explicit wait or wait for page to load. This is just for example
List <WebElement> buttons = driver.findElements(By.tagName("button"));
for (int i=0; i<buttons.size();i++){
WebElement button = buttons.get(i);
try {
if(button.isDisplayed()){
System.out.println(button.getText()+" - is displayed");
}
else {
System.out.println(button.getText()+" - is present but not displayed");
}
} catch(NoSuchElementException e) {
System.out.println("Element is not present, hence not displayed as well");
}
}
- 使用一些等待功能来正确加载页面。
- 使用
isDisplayed
方法检查按钮的可见性。isDisplayed
当元素不存在且不可见时将引发错误。所以使用 try catch 块来处理NoSuchElementException
异常。 - 您正在打印
WebElements
对象,即按钮。使用按钮的获取文本方法,然后打印它。您可以看到哪个按钮可见或哪个不可见。
推荐阅读
- json - OpenWeatherMap 和 Swift 4
- python - 如何在 Python 中将二维数组更改为一维数组
- r - 将数据框写入多个excel文件
- php - 使用 PHP 从 MySql 表创建子菜单
- kubernetes - 将 Google Cloud SQL 凭据连接到 Kubernetes Secret
- python - 从 0.5.1 升级到 0.8.2 后 graph_from_polygon 中的 osmnx TypeError
- arrays - 字符串的动态数组跳过第一个索引
- typescript - 返回扩展匿名类的函数的类型定义
- r - R:无法将绘图从循环输出到文件
- javascript - x 不是一个文件中的函数,但它可以在另一个文件中使用?