1

I need to close all open Chrome drivers in Selenium. All my methods are closing only one of them. The reason why I need to close all drivers at the same time - in the start of my program I don't know how many drivers I need to open, so I try to open a few drivers with same driver names in cycle.

How I opened these drivers:

WebDriver driver = new ChromeDriver();
driver.get(firstURL);

driver = new ChromeDriver();
driver.get(secondURL);

How I tried to close both drivers:

First try:

driver.close();

Second try:

driver.quit();

Third try:

driver.close();
driver.close();

Fourth try:

driver.quit();
driver.quit();
Stas Mackarow
  • 175
  • 2
  • 5
  • 16
  • 1
    Possible duplicate of [Difference between webdriver.Dispose(), .Close() and .Quit()](http://stackoverflow.com/questions/15067107/difference-between-webdriver-dispose-close-and-quit) – Jainish Kapadia Mar 29 '17 at 10:32
  • Nope. These methods are closing only one window in one driver, but i have a few drivers with same names and i need to close them all – Stas Mackarow Mar 29 '17 at 10:57
  • Why not closing your current driver before assigning a new instance? – Adonis Mar 29 '17 at 11:22
  • Why do you need more than one driver? What you are doing is not creating more than one driver, you are just launching a new browser instance under the same old variable which is orphaning the previous browsers. – JeffC Mar 29 '17 at 15:10

7 Answers7

1

There is only a single WebDriver driver in your Code, even though you are assigning it multiple ChromeDriver() objects. So You can just close the driver once either by using driver.close() or driver.quit();

and only the latest window will be closed all the previous windows will still remain (now which can't be contacted anymore) as the only driver was closed.

Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
1

First let's look at your code:

WebDriver driver = new ChromeDriver();
driver.get(firstURL);

driver = new ChromeDriver();
driver.get(secondURL);

Here is what is happening:

  1. You open a new Chrome instance.
  2. You then do something with that instance: navigate to a website.
  3. The next step, several things are happening: you open a new Chrome instance, and you overwrite the reference to the previous instance you opened. Essentially, at this point you lost track of the first browser you just opened!
  4. You now do something with the second opened instance: navigate to a website.

From you question it is not clear what exactly you are trying to accomplish. You have several options:

  1. Open one instance of a browser before your test. If you are using, for example JUnit, this is often done in the @Before method.
  2. Do some work in your test.
  3. Close the browser after the test. Again in JUnit, this wold be done in the @After method.

Another alternative is that you may legitimately need multiple browsers. You will need to keep track of all of them.

  1. You could create a List of drivers, and every time you open a new one, add it to that list.
  2. At the end of your tests, iterate over that list, and close all of them.
SiKing
  • 10,003
  • 10
  • 39
  • 90
0

How did you initialize your driver. Did you initialize more than one driver? If so, then use quit() method for all drivers separately. Otherwise, only driver.quit() should work.

Edit:

Use driver.quit() before each new assignment.

JeffC
  • 22,180
  • 5
  • 32
  • 55
optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
  • Yes, but all drivers with the same names, because when i start my program - i don't know how much drivers i need to initialize. That's the reason why i trying to initialize them in cycle. – Stas Mackarow Mar 29 '17 at 10:32
0

driver.quit() will close all (parent+child) browser windows and end the whole session. This should work fine.

Robert Langdon
  • 855
  • 1
  • 11
  • 27
boooom
  • 9
  • 2
0

I have a couple suggestions for you.

1: Naming Convention

In your post, you said:

"...in the start of my program I don't know how many drivers I need to open, so I try to open a few drivers with same driver names in cycle."

This is not a very good approach and here's why: let's say that you have two WebDrivers, each named "driverX". You need the first driver to go to "www.google.com", and you need the second driver to go to "www.yahoo.com". As such, your code looks like this:

driverX.get("www.google.com");
driverX.get("www.yahoo.com");

This becomes a problem, as instead of telling the second WebDriver to go to Yahoo, you have actually rerouted the first WebDriver from Google to Yahoo.

What is the solution to this problem? Name each of your WebDrivers with a unique name (googleDriver or yahooDriver for example). Then, your code will look like this and you should not have any conflicts:

googleDriver.get("www.google.com");
yahooDriver.get("www.yahoo.com");

2: WebDriver Grouping

Now that we have each of your WebDrivers with a unique name, we are able to reference them individually. This is great, however, some applications require that we reference several of them at once. This is where ArrayLists can come in. In the code below, I used an ArrayList to solve your original question.

ArrayList<WebDriver> activeDrivers = new ArrayList<>();
activeDrivers.add(googleDriver);
activeDrivers.add(yahooDriver);

for(WebDriver driver : activeDrivers){
    if(driver.getCurrentUrl().equals("Some predefined exit page"){
       driver.quit();
    }
}

Feel free to leave a comment if you still do not understand or if you have any questions.

0
@AfterMethod
public void tearDown(){

// Your Code

// At the End call

driver.close();

}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Suuresh
  • 11
  • 3
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value – Jose Da Silva Gomes Apr 07 '18 at 17:10
-1

To expand on Zachary's answer, assuming you re using drivers in a test context, you can:

  • Create an arraylist in the test class
  • add the drivers you create inside the test methods to this list
  • add an @After method containing a for each loop calling quit() on all drivers dynamically added to the arraylist during tests
Leon
  • 408
  • 1
  • 6
  • 17