c# - 保存 chrome cookies Selenium

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

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

最佳答案

您需要在所需的用户配置文件下运行 Chrome。默认情况下,Selenium 网络驱动程序将创建一个临时配置文件。如果您为其提供用户配置文件,该配置文件将保留,如果 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.

关于c# - 保存 chrome cookies Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39183753/

相关文章:

c# - 如何在没有构造函数和静态方法的情况下描述 C# 接口(interface)?

Java selenium 避免 iframe 地址栏元素

c# - Array.ForEach 无法从用法推断数组元素类型

c# - XslCompiledTransform 与 XslTransform 以及 OOM 的比较好吗?

c# - 如何在C#中实现维特比算法来拆分连体词?

testing - 从串行运行 Selenium 测试到并行运行时处理测试数据

python - 默认调用父构造函数?

asp.net - htmlTableRow 不在设计器中

c# - 排除以特定字符开头的正则表达式匹配

VB.NET 2005 - "Global"事件处理程序?