无法在 Selenium Webdriver 中设置 cookie [英] Unable to set cookies in Selenium Webdriver

查看:90
本文介绍了无法在 Selenium Webdriver 中设置 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

请在下面找到我的代码:

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.

推荐答案

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

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.

从我之前的阅读来看,最好的方法是导航到某个您知道域中不存在的页面,例如www.example.com/this404page,然后创建 cookie.它应该加载得更快,因为它是一个错误页面并且不应该包含太多内容.在 404 页面上创建 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.

这篇关于无法在 Selenium Webdriver 中设置 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆