Closing browser not driver, please suggest

121 views
Skip to first unread message

Ashvini Sharma

unread,
Feb 7, 2015, 8:28:23 AM2/7/15
to webd...@googlegroups.com
Hi,

Once I execute selenium code/functionality, I want to close browser.
Therefore, I used "driver.close();" and its closed the browser window. (I am using chrome webdriver.)

However, I want to reopen my browser for next functional code execution. This code executes:
But I am getting exception, "VisitURL failed because of: no such session".

Looks like "driver.close" is closing the driver too and I need to reinitialize webdriver something like "WebDriver driver = new ChromeDriver();".
Now, I do not want to call this webdriver initialization. My motive is just to close browser and reopen it.
Please suggest.

Thanks,
Ashvini

Selenium Framework

unread,
Feb 7, 2015, 5:36:16 PM2/7/15
to webd...@googlegroups.com
You might be looking for something like this (If you are running on windows, open  your task manager and observe at what points of the code chromedriver.exe exists and when it gets killed. Driver in java code requests a session from the server i.e. chromedriver.exe, so driver.close() doesn't kill the server, it just kills the session)

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;


import java.io.File;
import java.io.IOException;


public class SimpleTest {
   
public static void main(String args[]) throws IOException {
       
ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File("C:\\seleniumdrivers\\chromedriver.exe")).usingAnyFreePort().build();
        service
.start();
       
WebDriver driver = new ChromeDriver(service,new DesiredCapabilities().chrome());
       
System.out.println("First Driver Session id -- "+((RemoteWebDriver)driver).getSessionId());
        driver
.get("http://www.seleniumframework.com");
       
System.out.println("Title-"+driver.getTitle());
        driver
.close();
        driver
= new ChromeDriver(service,new DesiredCapabilities().chrome());
       
System.out.println("Second Driver Session id -- "+((RemoteWebDriver)driver).getSessionId());
        driver
.get("http://automationpractice.com");
       
System.out.println("Automation practice title-"+driver.getTitle());
        driver
.close();
        service
.stop();
       
   
}
}


Cheers,

Ashvini Sharma

unread,
Feb 9, 2015, 8:04:30 AM2/9/15
to webd...@googlegroups.com
Thank you for your response. 
Apparently you are doing the same on line no. 21.
driver = new ChromeDriver(service,new DesiredCapabilities().chrome());

I searched on internet and unable to find any thing related to my concern. Looks like this is how selenium works, no explicit way to open browser.
My scenario is: 
I want to execute a test case (let's searched something on an e-commerce site) and after that I want to close my browser, reopen it and execute 2nd test case.
In next cases, I want to execute 3rd test case and after that I do not want to close browser, just used new tab to execute 4th test case. 
 
I need to handle this scenario through code condition. I was thinking, if I could handle this just by closing and opening the browser. 

No worries, Appreciate your help, at least you replied and looked into my problem.
What I observed is, selenium opens browser by injecting driver in it, so whenever I need to open browser I need to use browser driver.

Thanks,
Ashvini

Andreas Tolfsen

unread,
Feb 9, 2015, 8:40:22 AM2/9/15
to webd...@googlegroups.com
On 7 Feb 2015, at 13:28, Ashvini Sharma <ashv....@gmail.com> wrote:
> Once I execute selenium code/functionality, I want to close browser.
> Therefore, I used "driver.close();" and its closed the browser window. (I am using chrome webdriver.)

[…]

> Looks like "driver.close" is closing the driver too and I need to reinitialize webdriver something like "WebDriver driver = new ChromeDriver();".
> Now, I do not want to call this webdriver initialization. My motive is just to close browser and reopen it.

If you take a look at the API documentation, it explicitly
says that calling close() on the last remaining open window
implicitly calls quit().

There's no way to recover from that, so the solution is to
create a new driver session.

Krishnan Mahadevan

unread,
Feb 9, 2015, 8:52:47 AM2/9/15
to webdriver
Ashvini,


I would be curious to know why you are bent upon this 

>>>> I want to execute a test case (let's searched something on an e-commerce site) and after that I want to close my browser, reopen it and execute 2nd test case. In next cases, I want to execute 3rd test case and after that I do not want to close browser, just used new tab to execute 4th test case. 

Why wouldnt just using the same browser and loading a new URL NOT work for you here ?
Opening a window as a new tab or as a new window I believe is a feature of the browser and NOT of the web application that you are testing. So how does it matter if you opened up your test in a browser window or a tab ?




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

Ashvini Sharma

unread,
Feb 9, 2015, 10:43:13 PM2/9/15
to webd...@googlegroups.com
Thanks @ato

murali seleniumtrainer

unread,
Feb 22, 2015, 7:57:13 AM2/22/15
to webd...@googlegroups.com
Hi Ashvini,

what about deleting the cookies..? Once test is completed..delete all cookies and start new one in same browser.. is it looks work..

Thank You
Reply all
Reply to author
Forward
0 new messages