Solution of chromedriver and geckodriver process not closed after Selenium execution of java Version + python Version

  • 2021-08-17 00:20:58
  • OfStack

selenium operation of the chrome browser requires the assistance of the ChromeDriver driver. There are two ways to close the browser in webdriver, one is called quit and the other is called close.


/**
  * Close the current window, quitting the browser if it's the last window currently open.
  */
 void close();

 /**
  * Quits this driver, closing every associated window.
  */
 void quit();

By looking at the above official statement document, we can see that close method is to close the current window. How to understand this at present? Is the page of driver instance operation, called current. If the current window has only 1 tab, then the close method is equivalent to closing the browser. The quit method simply exits and closes all associated tab windows. Therefore, close method 1 closes one tab, and quit method is what we think of as a complete browser shutdown method. To prove this, let's use an example to demonstrate:


package lessons;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FindElement_LinkText {

  public static void main(String[] args) throws Exception {

    System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();

    driver.get("https://www.baidu.com");

    driver.close();
    //driver.quit();
  }

}

Observe the actual effect of these two methods by switching the last two lines of code to log off and running them separately. When using close method, the browser will be closed because there is only tab on Baidu homepage, but by looking at the task manager, it is found that ChromeDriver process still exists in memory. If you use the quit method, the entire browser closes directly and the ChromeDriver process ends.

ChromeDriver is a lightweight service that normally ends the ChromeDriver process by using driver. quit () to close the browser on a single task or without frequent browser startup. If a large test suite is started and shut down frequently, an obvious delay will be added, which will lead to the browser process not being shut down. In order to avoid this situation, we can control the life and death of ChromeDriver process through ChromeDriverService, so as to achieve the effect of shutting down when it is used up and avoid the process occupation (Running the server in a child E43EN). The specific implementation is as follows:


ChromeDriverService service = new ChromeDriverService.Builder() .usingChromeDriverExecutable(new File("E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe")).usingAnyFreePort().build();
service.start();
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.quit();
//  Shut down  ChromeDriver  Interface 
service.stop();

The above discussion is the implementation of java version. For python, service library is used to control the opening and closing of chromedriver.


from selenium.webdriver.chrome.service import Service

When you create, you need to write the position of chromedriver. exe in the XXX part of Service, and you need to call his command line method, otherwise report an error, and then start it.


c_service = Service('xxx')
c_service.command_line_args()
c_service.start()
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")

When closing, use quit instead of close, close only closes the current page, quit exits the driver and closes all associated windows, and finally closes after execution.


driver.quit()
c_service.stop()

If you are too troublesome, you can also directly use the os module of python to execute the following two sentences to end the process


os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')

Related articles: