首页 > 解决方案 > 页面上的按钮列表。获得列表后,我想检查按钮是否显示在页面上

问题描述

我是自动化测试的新手,我整天都在努力解决这个问题。无论如何,我想去该网站并获取页面上的按钮列表。比我想打印页面上显示的按钮列表。

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]

标签: selenium-webdriverautomationautomated-tests

解决方案


基本上有三件事

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");
        }
    }
  1. 使用一些等待功能来正确加载页面。
  2. 使用isDisplayed方法检查按钮的可见性。isDisplayed当元素不存在且不可见时将引发错误。所以使用 try catch 块来处理NoSuchElementException异常。
  3. 您正在打印WebElements对象,即按钮。使用按钮的获取文本方法,然后打印它。您可以看到哪个按钮可见或哪个不可见。

推荐阅读