c# - 如何从 Chrome 驱动程序(Selenium Web 驱动程序 C#)中分离 Chrome 浏览器

标签 c# selenium selenium-webdriver selenium-chromedriver

我想在 VS 2010 C# 中使用 Selenium Web Driver 打开 Chrome 浏览器,导航到某个网页,然后关闭驱动程序但保持浏览器打开。我意识到之后我将不得不手动关闭浏览器,我对此表示同意。

到目前为止我有:

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]

我在最后一行尝试了以下所有内容

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

有什么想法吗?

最佳答案

我们可以使用“分离”选项从 chromedriver 分离 chrome 实例。

示例代码:

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

关于c# - 如何从 Chrome 驱动程序(Selenium Web 驱动程序 C#)中分离 Chrome 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14528700/

相关文章:

javascript - 单击链接时如何更新当前选项卡的 URL?

c# - WCF 4 - Soap 和 REST 端点

c# - LayoutKind.explicit 对于本身就是结构的字段的 .NET 行为

node.js - Docker:如何使用 selenium 服务器进行 nightwatchJS 测试?

javascript - 如何使用 Protractor 定位器定位子元素?

javascript - 如何通过 webdriverio wdio 文件更改 selenium-standalone 端口号?

html - 如何使用 Selenium 2 PHPUnit 获取所选单选按钮的文本

c# - 防止点击按钮时页面刷新

c# - 将特定数字从一个序列添加到另一个序列

selenium - 从浏览器使用 Selenium