org.openqa.selenium.InvalidCookieDomainException:使用Selenium和WebDriver禁止文档访问Cookie [英] org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse using Selenium and WebDriver

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

问题描述

我正在尝试将Cookie推送到上一个会话存储的Selenium firefox Webdriver中,但是出现错误:

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

我阅读了此 HTML Standard Cookie-averse 却一无所知.

所以,问题是如何将cookie推送到上一个存储的webdriver会话中?

解决方案

此错误消息...

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

...表示暗中尝试将cookie设置在与当前文档不同的域下.


详细信息

按照 HTML-Living标准规范在以下情况下,Document Object可能被归类为不喜欢Cookie的Document对象:

  • 没有 Browsing Context 的文档.
  • 其URL方案不是网络方案的文档.

深潜

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

例如:

  • 示例代码:

    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)
    

  • 控制台输出:

    InvalidCookieDomainException: Document is cookie-averse
    


解决方案

如果您存储的是来自域example.com的Cookie,则不能将这些存储的Cookie通过网络驱动程序会话推送到任何其他不同的域,例如example.edu.存储的Cookie只能在example.com中使用.此外,要在将来自动登录用户,您只需要存储一次Cookie,即用户登录后的时间.在添加Cookie之前,您需要浏览到收集Cookie的同一域.


示例

作为示例,一旦用户在应用程序中登录,就可以存储cookie,如下所示:

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()

稍后,如果要让用户自动登录,则需要先浏览到特定的域/url,然后必须按以下方式添加cookie:

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')


参考

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

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