应用程序在Selenium关闭和退出后没有停止。

huangapple go评论64阅读模式
英文:

The application is not stopped after selenium close and quit

问题

我有一个简单的网络爬虫应用程序。
```java
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");
    }

问题是在代码执行完毕后,应用程序并没有停止。可能的问题是什么,以及正确结束Selenium应用程序的方法是什么?


<details>
<summary>英文:</summary>

I have a simple web-scraping application.

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(&quot;https://www.tesco.com/groceries/en-GB/shop/fresh-food/all&quot;);
    String imageUrl = driver.findElement(By.className(&quot;product-image__container&quot;))
            .findElement(By.className(&quot;product-image&quot;)).getAttribute(&quot;src&quot;);
    System.out.println(imageUrl);
    driver.close();
    System.err.println(&quot;closed&quot;);

}
The problem is after the code is done the application is not stopped. What could be the problem and the proper way to end the selenium application?

</details>


# 答案1
**得分**: 1

将`driver.close()`更改为`driver.quit()`。这将销毁驱动程序并释放资源,不像`close()`只关闭当前窗口。

所以正常的做法是使用以下结构:

```java
try {
    // 这里是你的代码
} finally {
    if (driver != null) {
        driver.quit();
    }
}
英文:

Change driver.close() to driver.quit(). This will destroy the driver and release resources unlike close() which closes the current window.

So the normal practice would be using the construction like this:

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

huangapple
  • 本文由 发表于 2020年5月2日 17:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61557135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定