gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:58:32 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com