首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Chrome驱动程序和所有现有的浏览器cookie启动selenium?

问如何使用Chrome驱动程序和所有现有的浏览器cookie启动selenium?
EN

Stack Overflow用户
提问于 2016-04-26 11:19:56
回答 1查看 12.8K关注 0票数 12

据我所知,Chrome Driver总是在没有存储浏览器cookie的情况下启动。

我需要驱动程序从Chrome存储的所有cookie开始。

我想知道是否有任何方法可以用已经存储的cookie启动驱动程序?我正在使用带有.net 4.5的C#。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-26 12:11:29

是的,我们可以通过调用保存的chrome配置文件来实现,就像firefox配置文件一样。下面是我在以前的工作中记录的步骤

在Java中,我们可以通过使用ChromeOptions和Chrome Profile来实现。在chrome中,导航到chrome://version/,它将显示配置文件路径和可执行文件路径。

根据我在这方面的工作,配置文件路径是\Local\Google\Chrome\User Data\Profile 3,这显示了当我在普通的chrome浏览器中导航到chrome://version/时显示的内容。在此配置文件中,我导航到stackoverflow和saved credentials。所以在下面的代码中使用

代码语言:javascript
复制
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("binary", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

System.setProperty("webdriver.chrome.driver", "E:\\selenium_setups\\poi-3.12\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();

options.setExperimentalOption("prefs", prefs);
options.addArguments("user-data-dir=C:\\Users\\murali\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 3");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);

//WebDriver driver = new ChromeDriver(options);
driver.get("http://stackoverflow.com/");

据我所知,我希望登录时显示的是stackoverflow.com页面。但是第一次,我没有登录。因此,通过交叉检查chrome://version/,在驱动程序打开的chrome中,配置文件路径显示为\Local\Google\Chrome\User Data\Profile 3\Default。然后手动登录到该配置文件中,该配置文件由webdriver打开,并通过关闭它来执行gain。

最后,页面显示为已登录。所以它可能是在java中,我希望它能帮助你在C#中尝试一下。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36854674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档