操作系统:Windows 7 32bit
ChromeDriver版本:2.30
Selenium Webdriver版本:3.4.0
Java 8

我尝试了几种不同的方法来清理此代码,而不必重复相同的try / catch块。我正在尝试检查正在测试的页面上是否存在各种元素。我可以正常地向控制台报告,此代码可以正常工作。
我遇到的问题是不优雅的代码。有没有办法嵌套这些try / catch块,或将它们放在if / else循环中?

try {
            driver.findElement(By.xpath("/html/head/title"));
            System.out.println("Title found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("Title NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.xpath("//*[@id='logindiv']"));
            System.out.println("Login form found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("Login form NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.id("username"));
            System.out.println("'Username' field found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("'Username' form NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.id("password"));
            System.out.println("'Password' field found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("'Password' form NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.id("loginSubmit")).getText();
            System.out.println("Login button found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("Login button NOT FOUND...");
            // ex.printStackTrace();
        }

最佳答案

您可以尝试以下方法:

public void checkElementVisibile()throws InterruptedException {
    element = driver.findElement(By.xpath("/html/head/title"));
    Thread.sleep(1000);
    if(isElementVisibile(element))
        System.out.println("Title found...");
    else
        System.out.println("Title not found...");
    element = driver.findElement(By.xpath("//*[@id='logindiv']"));
    Thread.sleep(1000);
    if(isElementVisibile(element))
        System.out.println("Login form found...");
    else
        System.out.println("Login form not found...");
    element = driver.findElement(By.id("username"));
    Thread.sleep(1000);
    if(isElementVisibile(element))
        System.out.println("'Username' field found...");
    else
        System.out.println("'Username' field not found...");
    element = driver.findElement(By.id("password"));
    Thread.sleep(1000);
    if(isElementVisibile(element))
        System.out.println("'Password' field found...");
    else
        System.out.println("'Password' field not found...");
}
public static boolean isElementVisibile(WebElement element){
    try {
        return element.isDisplayed();
    }catch (NoSuchElementException ex)
    {
        return false;
    }
}

10-04 14:53