【问题标题】:Selenium Webdriver : How to get All cookies in the Current pageSelenium Webdriver:如何获取当前页面中的所有 cookie
【发布时间】:2014-07-23 13:46:28
【问题描述】:

尝试使用带有 java 的 Chrome 驱动程序获取当前页面中的所有 cookie。 请帮助我在关闭浏览器并尝试使用旧 cookie 打开新浏览器后检索页面中的所有 cookie。

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

    使用以下网址提供的 WebDriver API:

    http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Options.html

    获取当前页面的所有 cookie 并解析为 cookie 对象集合:

         driver.manage().getCookies();
        //TODO Parse results to Cookie Objects and Do what you want
    

    【讨论】:

      【解决方案2】:

      从所有域获取 cookie 在自动化测试中,我们可能需要验证网站的 cookie。

      Webdriver 具有简单而强大的 API 来检索当前页面域的 cookie。下面是读取 cookie 的示例代码:

      public Dictionary<string, string> GetAllPageCookies()
              {
                  return _driver.Manage().Cookies.AllCookies.ToDictionary(cookie => cookie.Name, cookie => cookie.Value);
              }
      

      【讨论】:

      • 这适用于您域中的 cookie,但不会为您提供来自第三方域的 cookie。所以不,不是所有的 cookie,尽管属性名称 :(
      猜你喜欢
      • 2012-07-17
      • 1970-01-01
      • 2012-11-04
      • 2018-02-24
      • 1970-01-01
      • 2013-07-17
      • 2013-04-05
      • 2011-03-24
      相关资源
      最近更新 更多