与另一个域的Selenium cookie

Muh*_*lan 8 java cookies selenium

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

Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
Run Code Online (Sandbox Code Playgroud)

我的代码:

//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);
    }
Run Code Online (Sandbox Code Playgroud)

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

ano*_*ave 8

为什么不在添加cookie之前将浏览器重定向到"example.com".进入该域后,添加您从"example.com"获取的cookie值并刷新页面?

根据团队在项目跟踪器上对此问题的回答,

Cookie方法仅对可见的cookie起作用,因为这是唯一可以在所有浏览器中一致地工作的方法.您看到的行为是预期的行为.

  • 你必须做一整页加载来设置一个cookie是非常荒谬的.如果您在多个域上有cookie,则必须为每个域执行页面加载. (4认同)

Nic*_*zza 8

就像之前的答案中提到的那样,这是预期的行为。

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

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

使 404 变通办法始终有效。

webdriver 规范 https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie强制当前浏览器会话位于您添加 cookie 的域上。

这很有道理,我同意。

不幸的是,这阻止了 2 个关键用例:

您希望在新会话中重复使用来自另一个 webdriver 会话的 cookie,以避免重复获取 cookie 所需的工作。例如必须重复登录。允许在不相关的线程之间共享 webdriver 池,其中每个线程可能有自己的 cookie。当前唯一的解决方法是尝试强制 webdriver 使用类似以下内容的 404 错误页面:webdriver.get(" http://the-cookie-domain.com/404adsfasdf ")。这将导致页面转到域并允许您使用 addCookie 添加 cookie。但这几乎永远不会奏效。由于该页面通常受 SSO 保护,因此任何访问http://the-cookie-domain.com/* 的尝试都会将您带到 SSO 登录页面,例如http://login.ssodomain.com 现在你有同样的问题。

我们应该向规范 webdriver.goTo404Page(domain) 添加一个新方法以允许此用例。此方法应模拟来自浏览器域的 404 HTTP 状态代码响应。

或者,可能是 addCookie 的新重载可以允许这样做,例如: void addCookie(Cookie var1, String goTo404PageOfDomain);

通过允许用户访问任何给定域的虚假 404 页面,这保证了 404 页面的解决方法适用于任何页面,并且这两个用例现在是可能的。

对于 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",
Run Code Online (Sandbox Code Playgroud)