org.openqa.selenium.InvalidCookieDomainException:使用 Selenium 和 WebDriver 的文档不喜欢 cookie [英] org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse using Selenium and WebDriver

查看:32
本文介绍了org.openqa.selenium.InvalidCookieDomainException:使用 Selenium 和 WebDriver 的文档不喜欢 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 cookie 推送到从上一个会话存储的 selenium firefox webdriver,但出现错误:

org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse

我阅读了这篇HTML 标准 Cookie-averse 什么都不懂.

那么,问题是如何将 cookie 推送到先前存储的 webdriver 会话?

解决方案

此错误信息...

org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse

...表示非法尝试在与当前文档不同的域下设置 cookie.


详情

根据 HTML 生活标准规范 在以下情况下,Document Object 可能被归类为不喜欢 cookie 的 Document 对象:

  • 没有浏览上下文的文档.
  • URL 方案不是网络方案的文档.

深入探讨

根据 无效 cookie 域,此错误可能当您访问不喜欢 cookie 的文档(例如本地磁盘上的文件)时发生.

举个例子:

  • 示例代码:

     from selenium import webdriverfrom selenium.common 导入异常session = webdriver.Firefox()session.get("file:///home/jdoe/document.html")尝试:foo_cookie = {name":foo",value":bar"}session.add_cookie(foo_cookie)除了 exceptions.InvalidCookieDomainException 作为 e:打印(e.message)

  • 控制台输出:

     InvalidCookieDomainException:文档不喜欢 cookie


解决方案

如果您已存储来自域 example.com 的 cookie,则这些存储的 cookie 不能通过 webdriver 会话推送到任何其他不同的域,例如example.edu.存储的 cookie 只能在 example.com 中使用.此外,为了将来自动登录用户,您只需要存储一次 cookie,那就是用户登录时.在添加回 cookie 之前,您需要浏览到收集 cookie 的同一个域.


示例

例如,您可以在用户登录应用程序后存储 cookie,如下所示:

from selenium import webdriver进口泡菜驱动程序 = webdriver.Chrome()driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')driver.find_element_by_name("username").send_keys("abc123")driver.find_element_by_name("password").send_keys("123xyz")driver.find_element_by_name("提交").click()# 存储cookiespickle.dump( driver.get_cookies() , open(cookies.pkl",wb"))驱动程序退出()

以后如果你想让用户自动登录,你需要先浏览到特定的域/url,然后你必须添加cookies如下:

from selenium import webdriver进口泡菜驱动程序 = webdriver.Chrome()driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')# 加载存储的 cookiecookies = pickle.load(open(cookies.pkl", rb"))对于 cookie 中的 cookie:# 通过 webdriver 实例将 cookie 添加到会话中driver.add_cookie(cookie)driver.get('http://demo.guru99.com/test/cookie/selenium_cookie.php')


参考

您可以在以下位置找到详细的讨论:

I'm trying to push cookies to selenium firefox webdriver stored from previous session, but I got error:

org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse

I read this HTML Standard Cookie-averse and understand nothing at all.

So, question is how to push cookies to webdriver session stored from previous one?

解决方案

This error message...

org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse

...implies that an illegal attempt was made to set a cookie under a different domain than that of the current document.


Details

As per the HTML-Living Standard Specification a Document Object may be categorized as a cookie-averse Document object in the following circumstances :

  • A Document that has no Browsing Context.
  • A Document whose URL's scheme is not a network scheme.

Deep Dive

As per Invalid cookie domain this error may occur when you visit a cookie-averse document, such as a file on your local disk.

As an example:

  • Sample Code:

      from selenium import webdriver
      from selenium.common import exceptions
    
      session = webdriver.Firefox()
      session.get("file:///home/jdoe/document.html")
      try:
          foo_cookie = {"name": "foo", "value": "bar"}
          session.add_cookie(foo_cookie)
      except exceptions.InvalidCookieDomainException as e:
          print(e.message)
    

  • Console Output:

      InvalidCookieDomainException: Document is cookie-averse
    


Solution

If you have stored the cookie from domain example.com, these stored cookies can't be pushed through the webdriver session to any other different domanin e.g. example.edu. The stored cookies can be used only within example.com. Further, to automatically login an user in future, you need to store the cookies only once, and that's when the user have logged in. Before adding back the cookies you need to browse to the same domain from where the cookies were collected.


Example

As an example, you can store the cookies once the user had logged in within an application as follows:

from selenium import webdriver
import pickle

driver = webdriver.Chrome()
driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')
driver.find_element_by_name("username").send_keys("abc123")
driver.find_element_by_name("password").send_keys("123xyz")
driver.find_element_by_name("submit").click()

# storing the cookies
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
driver.quit()

Later if you want the user automatically logged-in, you need to browse to the specific domain /url first and then you have to add the cookies as follows:

from selenium import webdriver
import pickle

driver = webdriver.Chrome()
driver.get('http://demo.guru99.com/test/cookie/selenium_aut.php')

# loading the stored cookies
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    # adding the cookies to the session through webdriver instance
    driver.add_cookie(cookie)
driver.get('http://demo.guru99.com/test/cookie/selenium_cookie.php')


Reference

You can find a detailed discussion in:

这篇关于org.openqa.selenium.InvalidCookieDomainException:使用 Selenium 和 WebDriver 的文档不喜欢 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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