vb.net 保存 chrome cookie Selenium

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39183753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:02:43  来源:igfitidea点击:

Saving chrome cookies Selenium

c#vb.netseleniumselenium-webdriverwebdriver

提问by Zach Johnson

I am looking for a way to save a chrome session cookies to persist even after my program has been closed. 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. Here is a bit of code:

我正在寻找一种方法来保存 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

回答by Alan

You need to run Chrome under the required user profile. By default, the Selenium web driver will create a temporary profile. 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。默认情况下,Selenium Web 驱动程序将创建一个临时配置文件。如果您为其提供用户配置文件,该配置文件将保留,如果 Chrome 未设置为删除 cookie,则将创建通常会为用户创建 cookie 的任何登录等。

See Selenium chromedriver sitefor 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.