简体   繁体   English

保存铬饼干硒

[英]Saving chrome cookies Selenium

I am looking for a way to save a chrome session cookies to persist even after my program has been closed. 我正在寻找一种方法来保存chrome会话cookie,即使我的程序关闭后,它们也可以持久保存。 I am assuming writing to a file would be a good way to go about doing this but I am at a loss as to how to accomplish this. 我假设写入文件将是执行此操作的好方法,但是我对如何完成此操作感到困惑。 My end goal is to save a login cookie so the user won't have to perform a login each time. 我的最终目标是保存登录cookie,以便用户不必每次都执行登录。 Here is a bit of code: 这是一些代码:

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

You need to run Chrome under the required user profile. 您需要在所需的用户个人资料下运行Chrome。 By default, the Selenium web driver will create a temporary profile. 默认情况下,Selenium Web驱动程序将创建一个临时配置文件。 If you give it a user profile, the profile will persist and if Chrome is not set to delete cookies, any logins etc. that would normally create a cookie for a user will be created. 如果您为其提供用户个人资料,则该个人资料将保留下来,并且如果未将Chrome设置为删除Cookie,则会创建通常会为用户创建Cookie的所有登录信息。

See Selenium chromedriver site for details: 有关详细信息,请参见Selenium chromedriver网站

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM