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

查看:174
本文介绍了无法在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)

请找到我的代码下面:

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天全站免登陆