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

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

问题描述

我正在尝试将 cookie 推送到之前会话中存储的 selenium firefox webdriver,但出现错误:

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

我读过这个 HTML 标准 Cookie-averse 什么都不懂.

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

那么,问题是如何将 cookie 推送到之前存储的 webdriver session 中?

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

推荐答案

这个错误信息...

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

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

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

根据 HTML-Living 标准规范 Document Object 在下列情况下可能被归类为 cookie-averse Document 对象:

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

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

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

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

举个例子:

  • 示例代码:

  • 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
    

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

    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.

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

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

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

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

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