Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shutting down Selenium without closing the browser, while cleaning up temporary files

Tags:

java

selenium

I have a set of classes written in Java using Selenium WebDriver for automating various tasks on a number of different websites such as logging in and fetching data.

From time to time we would need to access the page ourselves to find something, and the process of logging in manually is pretty...well, boring and a waste of time.

I figured since we already have the login macro, why don't we just let selenium handle it.

I would like to open a new browser, do the login procedures, and then shut down selenium. The user would then resume whatever tasks they wanted to do on the site.

I've noticed that if I don't properly shut down Selenium, it leaves an anonymous profile in the temp folder. Overtime, I've accumulated about a gig's worth of profiles before I realized this was happening. Turns out it was because I wasn't properly shutting down selenium using close and quit

However, if I use those methods, the browser is closed with it. Just now I have tested as follows

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.close()  // shuts down the browser

Then I tried

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.quit() // shuts down the browser

Is there a way for me to shut down Selenium without closing the browser while cleaning up all these anonymous profiles? I'm basically using it as a macro, except I launch it programmatically through our Java application that manages all the different macros.

like image 501
MxLDevs Avatar asked Sep 02 '14 15:09

MxLDevs


People also ask

What is the difference between quit () and close () method in Selenium?

The difference between quit() and close() driver. quit() : The quit() method quits the driver, closing every associated window. driver. close() : The close() method closes the currently focused window, quitting the driver if the current window is the only open window.

Which method closes the open browser in WebDriver quit () Terminate () close () shutdown ()?

quit() method closes all browser windows and ends the WebDriver session.

Can you run Selenium in the background?

Selenium Webdriver can run in the background, i.e. without opening a browser window, by running it in Headless mode. To do this you need to add the required capability to the set-up code for the driver.


1 Answers

After your code is done, kill the executable process and then exit the program without driver.quit()

    try{
    /*kill the process*/
        Runtime.getRuntime().exec("taskkill /im chromedriver.exe /f");
        Thread.sleep(10000);
    }catch(Exception e){
        e.printStackTrace();

    }
    /*Exit*/
    System.exit(0)
like image 73
Harit Yadav Avatar answered Oct 13 '22 10:10

Harit Yadav