与另一个域的硒cookie [英] Selenium cookie with another domain

查看:152
本文介绍了与另一个域的硒cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于selenium的代码来测试一个表单。但首先我去另一个页面,然后重定向到我的页面。当我设置cookie到新域,我得到错误:

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 :

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获取/设置Cookie

How can i manage that ? I have to get/set cookies for example.com

推荐答案

为什么不让浏览器重定向到example.com添加Cookie。在网域上,添加您从example.com获取的Cookie值并刷新页面

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方法仅对可见的cookie起作用,
是唯一可以在所有
中一致工作的方法浏览器。您看到的行为是预期的行为。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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