【问题标题】:How to Detach Chrome Browser from Chrome Driver (Selenium Web Driver C#)如何从 Chrome 驱动程序中分离 Chrome 浏览器(Selenium Web Driver C#)
【发布时间】:2013-01-09 19:46:18
【问题描述】:

我想在 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

有什么想法吗?

【问题讨论】:

    标签: c# selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    我们可以使用“分离”选项从 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();
    

    【讨论】:

    • 我正在使用 Selenium.WebDriver 2.48 版。 “options.setExperimentalOption”不存在。另外,你确定那是 C# 吗?
    【解决方案2】:

    这根本不可能,那种分离是不存在的。

    【讨论】:

    • 那么为什么它会记录在 ChromeDriver 网站上? - sites.google.com/a/chromium.org/chromedriver/capabilities(见“分离”)
    • @JaySullivan 因为这个答案是在 ChromeDriver 处于起步阶段并且无法支持它时给出的(并且可能是现在唯一支持它的)。
    • 有趣的是,尽管 ChromeDrive 记录了它,但我测试了 detach 并发现它不起作用。很难说我是不是做错了——到目前为止还没有看到其他人在网上声称成功。
    【解决方案3】:

    chromeservice.driver.close()

    过去曾为我工作过,但在这种情况下,您可能需要为方法编写一些代码。

    【讨论】:

    • 也许您可以添加带有编码建议的代码 sn-p。
    【解决方案4】:

    至少在 c# 中你需要下一行:

    driverOptions.AddExcludedArgument("enable-automation");
    driverOptions.AddAdditionalCapability("useAutomationExtension", false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 2020-05-11
      相关资源
      最近更新 更多