Java Selenium cookie与另一个域
我有一个关于selenium的代码来测试表单。但首先我转到另一个页面,然后重定向到我的页面。当我将Cookie设置为新域时,出现错误:Java Selenium cookie与另一个域,java,cookies,selenium,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
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,example将我重定向到“example.com”所有cookie域都是“example.com”
驱动程序。获取(“http://www.example.com?id=1");
设置cookies=driver.manage().getCookies();
迭代器itr=cookies.Iterator();
while(itr.hasNext()){
Cookie c=itr.next();
System.out.println(“Cookie名称:+c.getName()+”--“+”Cookie域:+c.getDomain()+”--“+”Cookie值:+c.getValue());
driver.manage().addCookie(c);
}
我该怎么办?我必须获取/设置cookies for example.com为什么不在添加cookies之前将浏览器重定向到“example.com”。进入该域后,添加从“example.com”获取的cookie值,然后刷新页面 根据“项目追踪者”上的信息 Cookie方法仅作用于可视的Cookie,如下所示 是唯一能够在所有领域保持一致工作的东西 浏览器。您看到的行为是预期的行为
正如前面回答中提到的,这是预期行为 到目前为止,唯一的解决办法是
driver.get(“domain.com/404”)
page。但由于SSO经常保护domain.com/*
我对规格提出了新的功能要求:
使404解决方案始终有效
webdriver规范
军队
要添加的域上的当前浏览器会话
把饼干给我
这很有道理,我同意
不幸的是,这阻止了两个关键用例:
您想在新的应用程序中重新使用来自另一个webdriver会话的Cookie
会话以避免重复获取Cookie所需的工作。这样的
因为必须重复登录。允许共享webdriver池
在不相关的线程中,每个线程可能有自己的
饼干。目前唯一的解决办法是尝试强制
webdriver转到404错误页面,其内容如下:
webdriver.get(“”)。这会
使页面转到域,并允许您使用添加cookie
阿德库奇。但这几乎不起作用。因为页面经常是
受SSO保护,任何尝试转到*
将您发送到SSO登录页面,例如现在
你也有同样的问题
我们应该向spec webdriver.goTo404Page(域)添加一个新方法
允许这个用例。此方法应模拟404HTTP状态
浏览器中来自域的代码响应
或者可能是addCookie的一个新重载可以允许这样做,
例如:void addCookie(Cookie var1,字符串goTo404PageOfDomain)
通过允许用户进入任何给定域的假404页面,这
保证404页解决方案适用于任何页面和这2种用途
现在有可能出现这种情况
对于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",
根据错误:
您只能为当前域设置cookie
您不能这样做。您必须进行整页加载才能设置cookie,这非常可笑。如果你有几个域上的cookie,你就必须为每个域加载页面。@speedplane:公平地说,对于编写Selenium的人来说,我不认为这是荒谬的——假设他们有很多个人的浏览器特性需要解决。如果您的需求更为复杂,您可以在测试套件之外进行设置-例如,创建一个cookie文件或将cookie文件传递给PhantomJS,等等。我同意。这很可笑。