无头chromedriver的饼干尺寸0

时间:2018-07-20 14:04:28

标签: java selenium-webdriver selenium-chromedriver integration-testing

我在Java测试类中遇到无头chromedriver问题。如果我使用无头选项运行Java测试,则driver.manage()。getCookies();返回空集。如果禁用无头选项,则可以找到所需的jsession和其他cookie。有人可以帮助我从无头浏览器获取Cookie。 我的代码如下:

尝试{

  ChromeOptions options = new ChromeOptions();
  options.setHeadless(true);

  DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  capabilities.setCapability(ChromeOptions.CAPABILITY, options);

  ChromeDriver driver = new ChromeDriver(capabilities);
  driver.get(HOME_PAGE_URL);

  Thread.sleep(5000);

  Set<Cookie> cookies = driver.manage().getCookies();
  System.out.println("cookies size " + cookies.size());
  Iterator<Cookie> itr = cookies.iterator();
  while (itr.hasNext()) {
    Cookie cookie = itr.next();
    String cookieName = cookie.getName();
    System.out.println(cookieName + "=====" + cookie.getValue());
  }

} catch (Exception e) {
  e.printStackTrace();
}

0 个答案:

没有答案