保存铬饼干Selenium

时间:2016-08-27 17:15:36

标签: c# vb.net selenium selenium-webdriver webdriver

我正在寻找一种方法来保存chrome会话cookie,即使我的程序关闭后仍然存在。我假设写一个文件是一个很好的方法来做这个,但我不知道如何实现这一目标。我的最终目标是保存登录cookie,这样用户每次都不必执行登录。这是一些代码:

Dim driver = New Chrome.ChromeDriver()
driver.Navigate.GoToUrl("URL")
'click stuff and login here
Dim _cookies = driver.Manage().Cookies.AllCookies
'write cookies to file or save somehow

1 个答案:

答案 0 :(得分:10)

您需要在所需的用户个人资料下运行Chrome。默认情况下,Selenium Web驱动程序将创建临时配置文件。如果您为其提供用户个人资料,则该个人资料将保留,如果Chrome未设置为删除Cookie,则会创建通常会为用户创建Cookie的任何登录等。

有关详细信息,请参阅Selenium chromedriver site

Use custom profile (also called user data directory)
By default, ChromeDriver will create a new temporary profile for each session. At times you may want to set special preferences or just use a custom profile altogether. If the former, you can use the 'chrome.prefs' capability (described later below) to specify preferences that will be applied after Chrome starts. If the latter, you can use the user-data-dir Chrome command-line switch to tell Chrome which profile to use:

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");

You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the user-data-dir switch set to some new directory. If the path doesn't exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open chrome://version in the browser to see what profile Chrome is using.