Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Detach Chrome Browser from Chrome Driver (Selenium Web Driver C#)

I want to use Selenium Web Driver in VS 2010 C# to open a Chrome browser, navigate to some web page and then close the driver but keep the browser open. I realize that I will have to manually close the browser afterwards and I'm okay with that.

So far I have:

DriverService service = ChromeDriverService.CreateDefaultService();
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("chrome.detach",true);
m_driver = new ChromeDriver(service, options, TimeSpan.FromMilliseconds(1000));
[m_driver does stuff like navigate page, double click stuff, etc]
[last line: try to close driver but not browser]

I have tried all the following as the last line

m_driver.Dispose(); // closes both browser and driver

m_driver.Close(); //closes just the browser and not the driver

m_driver.Quit(); // closes both browser and driver

service.Dispose(); // closes both browser and driver

Any ideas?

like image 743
san5 Avatar asked Jan 25 '13 19:01

san5


People also ask

How do I change my browser driver path in Selenium?

Go to the terminal and type the command: sudo nano /etc/paths. Enter the password. At the bottom of the file, add the path of your ChromeDriver. Type Y to save.

What is the difference between web Driver and Chrome driver?

A ChromeDriver is a separate executable or a standalone server that Selenium WebDriver uses to launch Google Chrome. Here, a WebDriver refers to a collection of APIs used to automate the testing of web applications.


1 Answers

We can detach chrome instance from chromedriver using "detach" options.

Sample Code:

ChromeDriverService cdservice = new ChromeDriverService.Builder()
                .usingDriverExecutable(new File("/path/to/chromedriver.exe"))
                .withLogFile(new File("/path/to/chromedriver.log"))
                .usingAnyFreePort().withVerbose(true).build();
cdservice.start();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("detach", true);
ChromeDriver driver = new ChromeDriver(cdservice,options);
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
driver.get("http://www.google.com/");

// Do not call driver.quit().. instead stop chromedriver service.
cdservice.stop();
like image 99
srees Avatar answered Nov 09 '22 03:11

srees