java - 具有另一个域的 Selenium cookie

标签 java cookies selenium

我有一个关于 selenium 的代码来测试一个表单。但首先我转到另一个页面,然后重定向到我的页面。当我将 cookie 设置到新域时,出现错误:

Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain

我的代码:

//it is going to example.com and example redirect me to the "example.com" all cookie domains is "example.com"
driver.get("http://www.example.com?id=1");

 Set<Cookie> cookies = driver.manage().getCookies();
 Iterator<Cookie> itr = cookies.iterator();

    while (itr.hasNext()){
    Cookie c = itr.next();
    System.out.println("Cookie Name: " + c.getName() + " --- " + "Cookie Domain: " + c.getDomain() + " --- " + "Cookie Value: " + c.getValue());

    driver.manage().addCookie(c);
    }

我该如何管理?我必须为 example.com 获取/设置 cookie

最佳答案

如之前的回答所述,这是预期的行为。

迄今为止唯一的解决方法是 driver.get("domain.com/404") 页面。但这并不总是有效,因为 SSO 经常保护 domain.com/*

我已针对规范提出新功能请求:https://github.com/w3c/webdriver/issues/1238

使 404 解决方法始终有效。

The webdriver spec https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie forces the current browser session to be on the domain where you are adding the cookie to.

This makes tons of sense and I agree with it.

This unfortunately prevents 2 key use cases:

You want to re-use cookies from another webdriver session in a new session to avoid repeating the work it took to get the cookies. Such as having to repeat a login. Allow a webdriver pool to be shared amongst unrelated threads where each thread might have their own cookies. The only current work around is to attempt to force the webdriver to go to a 404 error page with something like: webdriver.get("http://the-cookie-domain.com/404adsfasdf"). This would cause the page to go to domain and would allow you to add cookies with addCookie. But this hardly ever works. Because the page is very often protected by SSO, any attempt to go to http://the-cookie-domain.com/* sends you to an SSO login page e.g. http://login.ssodomain.com and now you have the same problem.

We should add a new method to the spec webdriver.goTo404Page(domain) to allow this use-case. This method should simulate a 404 HTTP status code response from domain in the browser.

Alternatively maybe be a new overload to addCookie could allow this, for example: void addCookie(Cookie var1, String goTo404PageOfDomain);

By allowing users to go to a fake 404 page of any given domain, this guarantees the 404-page workaround works for any page and these 2 use cases are now possible.

对于 firefox,您可以稍微修改 Marionette 以删除此检查。

diff --git a/testing/marionette/cookie.js b/testing/marionette/cookie.js
--- a/testing/marionette/cookie.js
+++ b/testing/marionette/cookie.js
@@ -144,14 +144,14 @@ cookie.add = function(newCookie, {restri
     newCookie.domain = "." + newCookie.domain;
   }

-  if (restrictToHost) {
-    if (!restrictToHost.endsWith(newCookie.domain) &&
-        ("." + restrictToHost) !== newCookie.domain &&
-        restrictToHost !== newCookie.domain) {
-      throw new InvalidCookieDomainError(`Cookies may only be set ` +
-          `for the current domain (${restrictToHost})`);
-    }
-  }
+//  if (restrictToHost) {
+//    if (!restrictToHost.endsWith(newCookie.domain) &&
+//        ("." + restrictToHost) !== newCookie.domain &&
+//       restrictToHost !== newCookie.domain) {
+//      throw new InvalidCookieDomainError(`Cookies may only be set ` +
+//          `for the current domain (${restrictToHost})`);
+//    }
+//  }

   // remove port from domain, if present.
   // unfortunately this catches IPv6 addresses by mistake
diff --git a/testing/marionette/driver.js b/testing/marionette/driver.js
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -2638,9 +2638,9 @@ GeckoDriver.prototype.addCookie = functi
   let {protocol, hostname} = this.currentURL;

   const networkSchemes = ["ftp:", "http:", "https:"];
-  if (!networkSchemes.includes(protocol)) {
-    throw new InvalidCookieDomainError("Document is cookie-averse");
-  }
+//  if (!networkSchemes.includes(protocol)) {
+//    throw new InvalidCookieDomainError("Document is cookie-averse");
+//  }

   let newCookie = cookie.fromJSON(cmd.parameters.cookie);

diff --git a/testing/marionette/test_cookie.js b/testing/marionette/test_cookie.js
--- a/testing/marionette/test_cookie.js
+++ b/testing/marionette/test_cookie.js
@@ -190,10 +190,10 @@ add_test(function test_add() {
   });
   equal(2, cookie.manager.cookies.length);

-  Assert.throws(() => {
-    let biscuit = {name: "name3", value: "value3", domain: "domain3"};
-    cookie.add(biscuit, {restrictToHost: "other domain"});
-  }, /Cookies may only be set for the current domain/);
+//  Assert.throws(() => {
+//    let biscuit = {name: "name3", value: "value3", domain: "domain3"};
+//    cookie.add(biscuit, {restrictToHost: "other domain"});
+//  }, /Cookies may only be set for the current domain/);

   cookie.add({
     name: "name4",

关于java - 具有另一个域的 Selenium cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22507204/

相关文章:

linux - NGINX cookie 免费域配置设置小问题

java - 如何存储列表中多个项目的全局数组以供以后使用

页面刷新期间的 selenium.common.exceptions.StaleElementReferenceException

java - 在JPA中,如何知道entityManager.persist(obj)是否已将对象持久化到数据库中?

c# - 使用 WebBrowser 类 C# 获取/设置 cookie

java - 如何在 Eclipse 中针对 Tomcat 正确部署和构建项目?

amazon-web-services - 应用程序负载均衡器 session cookie 的过期时间非常高

python - selenium.common.exceptions.WebDriverException : Message: chrome not reachable error while using find_element_by_id Selenium with ChromeDriver

java - 对象变量无法识别

java - 如何在短时间内连续移动?