硒关闭并退出后,应用程序未停止

时间:2020-05-02 09:28:06

标签: java selenium

我有一个简单的Web爬网应用程序。

public static void main(String[] args) throws InterruptedException {
        File file = new File("C:/Users/Евгений/Desktop/chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.tesco.com/groceries/en-GB/shop/fresh-food/all");
        String imageUrl = driver.findElement(By.className("product-image__container"))
                .findElement(By.className("product-image")).getAttribute("src");
        System.out.println(imageUrl);
        driver.close();
        System.err.println("closed");

    }

问题是在完成代码后,应用程序并未停止。可能会出现问题,并且终止硒应用的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

driver.close()更改为driver.quit()。与close()会关闭当前窗口不同,这将破坏驱动程序并释放资源。

所以通常的做法是使用这样的结构:

try {
    // your code here
}finally {
    if (driver != null) {
        driver.quit();
    }
}
相关问题