如何设置一个cookie在python的硒webdriver中的特定域? [英] How to set a cookie to a specific domain in selenium webdriver with python?

查看:390
本文介绍了如何设置一个cookie在python的硒webdriver中的特定域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好同胞StackOverflow的用户。我试图实现的是防止当我的测试打开主页时出现恼人的帮助框弹出。到目前为止,这是我用来打开主页的方法:

  def open_url(self,url):
使用驱动程序的基本URL打开一个URL
self.webdriver.add_cookie({'name':'tour.index','value':'complete','domain':self。存储['base'] + url})
self.webdriver.add_cookie({'name':'tour.map','value':'complete','domain':self.store ['base' ] + url})
self.webdriver.get(self.store ['base'] + url)

然而,我运行测试后返回的是这样的:

  2014-07-23 15:38: 19.453057:X消息:你可以只为当前的域设置cookie。 

如何在实际加载基础测试域之前设置Cookie?

解决方案

文档建议在设置cookie之前导航到虚拟URL(例如404页面或图像的路径)。然后,设置cookie,然后导航到您的主页。



Selenium文档 - Cookies


...您需要位于cookie有效的域名上。如果您
正在尝试预先设置Cookie,然后再开始与网站
进行互动......另一种方法是
在网站上查找较小的网页...

所以,你的代码可能是这样的:

def open_url(self,url):
使用驱动程序的基本URL打开一个URL
$ b $ dummy_url ='/ 404error'
#或者这个
#dummy_url ='/ path / to /an/image.jpg'

#导航到相同域中的虚拟网址。
self.webdriver.get(self.store ['base'] + dummy_url)

#按照以前的方式继续
self.webdriver.add_cookie({'name':'tour (''name':'tour.map','value':'complete','domain':self.store ['base'] + url})
self.webdriver.add_cookie值':'complete','domain':self.store ['base'] + url})
self.webdriver.get(self.store ['base'] + url)


Hello fellow StackOverflow users. What I am trying to achieve is prevent annoying helper boxes from popping up when my tests open the main page. So far this is the method I am using to open the main page:

def open_url(self, url):
    """Open a URL using the driver's base URL"""
    self.webdriver.add_cookie({'name' : 'tour.index', 'value' : 'complete', 'domain' : self.store['base'] + url})
    self.webdriver.add_cookie({'name' : 'tour.map', 'value' : 'complete', 'domain' : self.store['base'] + url})
    self.webdriver.get(self.store['base'] + url)

However, what returns after I run the test is this:

2014-07-23 15:38:19.453057: X Message: u'You may only set cookies for the current domain' ;

How can I set the cookie before I actually load the base testing domain?

解决方案

The documentation suggests navigating to a dummy url (such as a 404 page, or the path to an image) before setting the cookies. Then, set the cookies, then navigate to your main page.

Selenium Documentation - Cookies

... you need to be on the domain that the cookie will be valid for. If you are trying to preset cookies before you start interacting with a site ... an alternative is to find a smaller page on the site ... (http://example.com/some404page)

So, your code might look like this:

def open_url(self, url):
    """Open a URL using the driver's base URL"""

    dummy_url = '/404error'
    # Or this
    #dummy_url = '/path/to/an/image.jpg'

    # Navigate to a dummy url on the same domain.
    self.webdriver.get(self.store['base'] + dummy_url)

    # Proceed as before
    self.webdriver.add_cookie({'name' : 'tour.index', 'value' : 'complete', 'domain' : self.store['base'] + url})
    self.webdriver.add_cookie({'name' : 'tour.map', 'value' : 'complete', 'domain' : self.store['base'] + url})
    self.webdriver.get(self.store['base'] + url)

这篇关于如何设置一个cookie在python的硒webdriver中的特定域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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