java - 处理 Selenium 抛出的错误

标签 java selenium error-handling

假设我有一个对象:

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class SOClass {
    private WebDriver driver;
    private List<String> dataString;


    public SOClass(WebDriver driver) throws ArrayIndexOutOfBoundsException {
        this.driver = driver;
        prepData();
    }

    private void prepData() throws ArrayIndexOutOfBoundsException {

        List<WebElement> data = this.driver.findElements(By.className("a-export-table"));
        if(data.isEmpty()) {
            throw new ArrayIndexOutOfBoundsException("There was no data in the table to export");
        }
        for(WebElement w : data) {
            this.dataString.add(w.getText());
        }
    }

    public void export(String path) throws IOException {
        FileWriter fw = new FileWriter(path);
        boolean isFirst = false;
        for(String s : this.dataString) {
            if(isFirst) {
                fw.append(s);
            } else {
                fw.append("," + s);
            }
        }
        fw.flush();
        fw.close();

    }
}

和主要:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class SOMain {
    private static final String COMPUTER_NAME = System.getProperty("user.name");
    private static final String CHROME_PATH =
            "C:/Users/" + COMPUTER_NAME + "/selenium/chromedriver.exe";

    private static final String OUT_PATH =  "C:/Users/" + COMPUTER_NAME + "/output/export.csv";

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", CHROME_PATH);
        ChromeOptions options = new ChromeOptions();
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new RemoteWebDriver(capabilities);
        driver.get("www.someurlexample.com");
        try {
            SOClass so = new SOClass(driver);
            so.export(OUT_PATH);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

现在,除非有任何编译问题(我编了一个例子),我的代码将捕获 SOClass 定义抛出的任何异常。但是,我想知道如果页面上不存在该表并且 Selenium 抛出 NoSuchElementException,我的 SOMain 是否会因为

而自动捕获此异常
 } catch(Exception e) {
 }

阻塞,或者因为没有指定对象抛出这个错误,SOMain不会处理这个错误并中断?

最佳答案

您的catch将处理从Exception类继承的每个异常(显然发生在try中),这意味着,包括NoSuchElementException 如您所见 here它的层次结构。

但是,您需要区分已检查异常未检查异常或运行时异常。正如您所看到的,NoSuchElementException 扩展了 java.lang.RuntimeException,这意味着它是未经检查的,因此编译器不需要您处理它。但请记住,此运行时异常扩展了 java.lang.Exception,因此您的 catch 将在运行时捕获它(如果发生)。

关于java - 处理 Selenium 抛出的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42445935/

相关文章:

windows - 将Powershell错误处理到批处理文件中

java - 通过调用方法打印右对齐三角形

java - Android 应用程序启动时间太长?

ruby - 在没有 RSpec 的情况下使用 Capybara live

java - 如何对 webelement 的子元素使用页面对象工厂框架

c# - try catch 异常错误

c# - Web API-自定义错误-即将推出

java - OSGi 声明式服务的绑定(bind)顺序

java - 在元音和辅音之间添加空格?

c# - Mstest C# Selenium 的 HTML 截图记者