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

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

问题描述

据我所知,Chrome驱动程序始终在没有存储任何浏览器cookie的情况下启动.

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

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

解决方案

是的,我们可以通过调用保存的chrome配置文件来实现它,就像firefox配置文件一样.以下是我刚才做某件事时记下的步骤

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

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

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/进行了交叉检查,显示了配置文件路径作为 \ Local \ Google \ Chrome \ User Data \ Profile 3 \ Default.然后手动在该配置文件中自行登录,该配置文件由webdriver打开,并通过关闭配置文件来执行获取.

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

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

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

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.

解决方案

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

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.

As per my working on this, The profile path is \Local\Google\Chrome\User Data\Profile 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/");

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 \Local\Google\Chrome\User Data\Profile 3\Default . then logged manually in that profile it self, which is opened by webdriver and executed gain by closing it.

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天全站免登陆