Java 无法在 Selenium Webdriver 中设置 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45842709/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:11:42  来源:igfitidea点击:

Unable to set cookies in Selenium Webdriver

javatestingcookiesselenium-webdrivermicrosoft-edge

提问by David.M

I'm trying to add cookies to a link before I open it with webdriver but it keeps giving me this error:

我试图在使用 webdriver 打开链接之前将 cookie 添加到链接,但它一直给我这个错误:

org.openqa.selenium.UnableToSetCookieException: Unable to set cookie (WARNING: The server did not provide any stacktrace information)

org.openqa.selenium.UnableToSetCookieException:无法设置 cookie(警告:服务器未提供任何堆栈跟踪信息)

Please find my code below:

请在下面找到我的代码:

System.setProperty("webdriver.edge.driver","C:\Program Files\Latest Webdriver\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example

Please help with a relevant solution.

请帮助提供相关解决方案。

回答by Kapil

Adding cookies before navigating to the domain are called domain-less cookies which i believe is not possible.

在导航到域之前添加 cookie 称为无域 cookie,我认为这是不可能的。

I have not found a way to drop a cookie before the url, but I think below scenario might help you out-

我还没有找到在 url 之前删除 cookie 的方法,但我认为下面的场景可能会帮助你 -

  1. Create a factory class to create webdriver instances
  2. Before returning the webdriver instance, navigating to any page on the domain under test, and drop the cookie, then navigate browser back.
  3. Your test can now begin, unaware that the navigation and cookie dropping has taken place
  1. 创建工厂类以创建 webdriver 实例
  2. 在返回 webdriver 实例之前,导航到被测域上的任何页面,并删除 cookie,然后导航回浏览器。
  3. 您的测试现在可以开始了,不知道导航和 cookie 丢弃已经发生

回答by JeffC

You are creating the cookie before navigating to the site. If you are trying to create a cookie on the domain www.example.com, then you would want to navigate to some page on that domain, create the cookie, and then start your test.

您在导航到该站点之前创建了 cookie。如果您尝试在域 www.example.com 上创建 cookie,那么您需要导航到该域上的某个页面,创建 cookie,然后开始测试。

From my reading a while back, the best way to do this is to navigate to some page you know will not exist on the domain, e.g. www.example.com/this404page, then create the cookie. It should load a lot faster since it's an error page and shouldn't contain much content. After creating the cookie on the 404 page, start your test.

根据我之前的阅读,最好的方法是导航到某个您知道域中不存在的页面,例如 www.example.com/this404page,然后创建 cookie。它应该加载得更快,因为它是一个错误页面并且不应该包含太多内容。在 404 页面上创建 cookie 后,开始测试。

回答by iamsankalp89

First navigate to URL and then try to add cookies, try below code:

首先导航到 URL,然后尝试添加 cookie,尝试以下代码:

System.setProperty("webdriver.edge.driver","C:\Program Files\Latest Webdriver\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example 

Replace your code with this :

用这个替换你的代码:

System.setProperty("webdriver.edge.driver","C:\Program Files\Latest Webdriver\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example 
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);

回答by Nicholas DiPiazza

You are unable to do this because the WebDriver spec requires that you have the browser landed at the domain you are trying to set cookies for.

您无法执行此操作,因为 WebDriver 规范要求您将浏览器登录到您尝试为其设置 cookie 的域。

The work-around as mentioned is to go to the page prior to setting the cookie. But that causes some issues:

上面提到的解决方法是在设置 cookie 之前转到页面。但这会导致一些问题:

This unfortunately prevents 2 key use cases:

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

  1. 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.
  2. Allow a webdriver pool to be shared amongst unrelated threads where each thread might have their own cookies.
  1. 您希望在新会话中重复使用来自另一个 webdriver 会话的 cookie,以避免重复获取 cookie 所需的工作。例如必须重复登录。
  2. 允许在不相关的线程之间共享 webdriver 池,其中每个线程可能有自己的 cookie。

The webdriver spec clearly needs to take this into account. I have opened an issue here:

webdriver 规范显然需要考虑到这一点。我在这里打开了一个问题:

https://github.com/w3c/webdriver/issues/1238

https://github.com/w3c/webdriver/issues/1238

Give it some votes. All browsers should start carrying a way to handle this.

给它一些选票。所有浏览器都应该开始携带一种方法来处理这个问题。

回答by nerak99

Just in case this is simpler, I did not need to create a factory class, merely load the root page from the site (which did not demand a cookie)

以防万一这更简单,我不需要创建工厂类,只需从站点加载根页面(不需要 cookie)

First collect the cookie as described above

首先按照上面的描述收集cookie

pickle.dump(driver.get_cookies(), open("ChromeCookies.pkl", "wb"))

Then get the web site root which does not require a cookie (in my case)

然后获取不需要 cookie 的网站根目录(在我的情况下)

driver.get(url_root)

Then execute the cookie load

然后执行cookie加载

for cookie in pickle.load(open("ChromeCookies.pkl", "rb")):
driver.add_cookie(cookie)

Then go to the page that I actually wanted to access

然后转到我真正想访问的页面

driver.get(url_not_root_demanding_cookie_which_is_now_there)

Please do post if there is an issue with this approach

如果这种方法有问题,请发帖