Java Selenium cookie 与另一个域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22507204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium cookie with another domain
提问by Muhammet Arslan
I have a code on selenium to test a form. But first i go to another page and then redirect to the my page. When i set cookies to new domain , i got error :
我有一个关于硒的代码来测试表单。但首先我转到另一个页面,然后重定向到我的页面。当我将 cookie 设置为新域时,出现错误:
Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
My Code :
我的代码:
//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);
}
How can i manage that ? I have to get/set cookies for example.com
我该如何处理?我必须为 example.com 获取/设置 cookie
采纳答案by anotherdave
Why not let the browser be redirected to "example.com" before adding the cookies. Once on that domain, add the cookie values you've taken from "example.com" and the refresh the page?
为什么不在添加 cookie 之前让浏览器重定向到“example.com”。进入该域后,添加您从“example.com”获取的 cookie 值并刷新页面?
As per the answer by the team on this issueon the project tracker,
The cookies methods only act on cookies that would be visible as this is the only thing that can be made to work consistently across all browsers. The behaviour that you see is the expected behaviour.
cookie 方法仅作用于可见的 cookie,因为这是唯一可以在所有浏览器上一致工作的方法。您看到的行为是预期的行为。
回答by Nicholas DiPiazza
Like mentioned in previous answer this is expected behavior.
就像之前的答案中提到的那样,这是预期的行为。
The only work around to date is to driver.get("domain.com/404")
page. But this doesn't always work due to SSO often protects domain.com/*
迄今为止唯一的解决方法是driver.get("domain.com/404")
翻页。但这并不总是有效,因为 SSO 经常保护domain.com/*
I have made a new feature request on the spec: https://github.com/w3c/webdriver/issues/1238
我对规范提出了新的功能请求:https: //github.com/w3c/webdriver/issues/1238
to make it so the 404 work-around can always work.
使 404 变通办法始终有效。
The webdriver spec https://w3c.github.io/webdriver/webdriver-spec.html#add-cookieforces 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.comand 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.
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 页面的解决方法适用于任何页面,并且这两个用例现在是可能的。
For firefox, you can modify Marionette slightly to just remove this check.
对于 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",