如何使用 Chrome 驱动程序和所有现有的浏览器 cookie 启动 selenium? [英] How do you start selenium using Chrome driver and all existing browser cookies?

查看:23
本文介绍了如何使用 Chrome 驱动程序和所有现有的浏览器 cookie 启动 selenium?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我目前了解,Chrome 驱动程序总是在没有任何存储的浏览器 cookie 的情况下启动.

From what I understand so far, Chrome Driver always starts without any stored browser cookies.

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

I need the driver start with all the cookies stored by Chrome.

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

I wonder if there is any way to start the driver with the cookies that are already stored? I'm using C# with .net 4.5.

推荐答案

是的,我们可以像 firefox 配置文件一样调用保存的 chrome 配置文件.以下是我之前做的时候记下的步骤

Yes we can do it by invoking saved chrome profile just like firefox profile. below are steps i noted when i am doing bit back ago

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

in Java, we can do it by using ChromeOptions and Chrome Profile. In chrome navigate to chrome://version/ It will display profile path and Executable path.

根据我的工作,配置文件路径是 LocalGoogleChromeUser DataProfile 3 这显示我导航到 chrome://时显示的内容version/ 在普通 chrome 浏览器中.在此配置文件中,我导航到 stackoverflow 并保存了凭据.所以在下面的代码中使用

As per my working on this, The profile path is LocalGoogleChromeUser DataProfile 3 This is displaying what is displayed when i navigate to chrome://version/ in normal chrome browser. In this profile, i navigated to stackoverflow and saved credentials. So used below code

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 中与 chrome://version/进行交叉检查,显示配置文件路径作为LocalGoogleChromeUser DataProfile 3Default .然后手动登录到它自己的配置文件,由 webdriver 打开并通过关闭它来执行获取.

As per my understanding, i excepted stackoverflow.com page displayed as logged in. but for first time, i am not logged in. so cross checked with chrome://version/ in chrome opened by driver, profile path is displayed as LocalGoogleChromeUser DataProfile 3Default . then logged manually in that profile it self, which is opened by webdriver and executed gain by closing it.

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

Finally, page is displayed as logged in. So it may be in java, i hope it will helps you to try in C# .

这篇关于如何使用 Chrome 驱动程序和所有现有的浏览器 cookie 启动 selenium?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆