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

天大地大妈咪最大 提交于 2021-02-07 03:03:42

问题


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?


回答1:


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();



回答2:


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




回答3:


chromeservice.driver.close()

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



来源:https://stackoverflow.com/questions/14528700/how-to-detach-chrome-browser-from-chrome-driver-selenium-web-driver-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!