【问题标题】:How do you start selenium using Chrome driver and all existing browser cookies?如何使用 Chrome 驱动程序和所有现有的浏览器 cookie 启动 selenium?
【发布时间】:2016-04-26 03:19:56
【问题描述】:

据我目前了解,Chrome 驱动程序总是在没有任何存储的浏览器 cookie 的情况下启动。

我需要驱动程序从 Chrome 存储的所有 cookie 开始。

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

【问题讨论】:

  • 我不确定你的假设是否正确。每当我启动 selenium webdriver 会话时,我的登录会话仍然存在。因此cookie应该在那里。

标签: selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

是的,我们可以通过调用已保存的 chrome 配置文件来做到这一点,就像 Firefox 配置文件一样。以下是我之前做的时候记下的步骤

在 Java 中,我们可以使用 ChromeOptions 和 Chrome Profile 来实现。在 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#中尝试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多