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

5

7

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?

Earful answered 25/1, 2013 at 19:7 Comment(0)
F
2

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();
Florrieflorry answered 12/1, 2016 at 7:46 Comment(1)
I'm using Selenium.WebDriver version 2.48. "options.setExperimentalOption" does not exist. Plus, are you sure that's C#?Quenelle
O
0

This is simply not possible, that kind of separation does not exist.

Oviduct answered 1/2, 2013 at 12:5 Comment(3)
Then why is it documented on the ChromeDriver website? - sites.google.com/a/chromium.org/chromedriver/capabilities (see "detach")Quenelle
@JaySullivan because this answer was given when ChromeDriver was in it's infancy and could not support it (and may be the only one that does right now).Oviduct
The funny thing is, even though ChromeDrive documents it, I've tested detach and found it not to work. Hard to say if I'm just doing it wrong—so far haven't seen anyone else online claiming success.Quenelle
K
0

chromeservice.driver.close()

has worked for me in the past but in this case you may need to write some coding for a method.

Kristine answered 27/8, 2014 at 11:19 Comment(1)
maybe you can add a code snippet with a coding suggestion.July
M
0

It is posible a least in c# you need the next lines:

driverOptions.AddExcludedArgument("enable-automation");
driverOptions.AddAdditionalCapability("useAutomationExtension", false);
Marilou answered 23/8, 2021 at 22:9 Comment(0)
C
0

(Python) When I called the selenium .get() function, Chrome would open up and close shortly after. I found online the advice to use the following code to remedy this issue: It worked for me in Python3; MacOSX 12.3 Monterey; Chrome Version 105.0.5195.102

from selenium import webdriver

options = webdriver.ChromeOptions()

options.add_experimental_option("detach", True)

driver = webdriver.Chrome(chrome_options=options, executable_path='/Users/<user_acct_name>/.wdm/drivers/chromedriver/mac64/105.0.5195/chromedriver')

Canorous answered 12/9, 2022 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.