利用splinter包增加cookie的问题

下面的一段代码是利用selenium+phantomjs的模拟浏览器并向当前访问的网站增加cookie值,代码如下所示:

from splinter import Browser

class dynamic(object):
    def __init__(self):
        self.request()

    def request(self):
        dicts = {'aaa':'bbb'}
        url = 'http://demo.aisec.cn/demo/aisec/'
        browser = Browser('phantomjs')
        browser.visit(url)
        browser.cookies.add(dicts)    
        print browser.cookies.all()
        browser.quit()

dynamic = dynamic()

但是测试的过程中出现了异常,异常如下所示。

Traceback (most recent call last):
  File "testcookie.py", line 17, in <module>
    dynaamic = dynamic()
  File "testcookie.py", line 6, in __init__
    self.request()
  File "testcookie.py", line 13, in request
    browser.cookies.add(dicts)
  File "build\bdist.win32\egg\splinter\driver\webdriver\cookie_manager.py", line 28, in ad
d
  File "C:\Python27\lib\site-packages\selenium-2.52.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py", line 634, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "C:\Python27\lib\site-packages\selenium-2.52.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.52.0-py2.7.egg\selenium\webdriver\remote\
errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: {"errorMessage":"Can only set Cook
ies for the current domain","request":{"headers":{"Accept":"application/json","Accept-Enco
ding":"identity","Connection":"close","Content-Length":"96","Content-Type":"application/js
on;charset=UTF-8","Host":"127.0.0.1:65104","User-Agent":"Python-urllib/2.7"},"httpVersion"
:"1.1","method":"POST","post":"{\"sessionId\": \"d26f62d0-ea95-11e5-a655-dfbbdd732124\", \
"cookie\": {\"name\": \"aaa\", \"value\": \"bbb\"}}","url":"/cookie","urlParsed":{"anchor"
:"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port
":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source"
:"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/d26f62d0-ea95-11e5-a
655-dfbbdd732124/cookie"}}
Screenshot: available via screen

大家可以参考splinter类向浏览器增加cookie的官方文档,发现所写的代码并没有什么错误。

http://splinter.readthedocs.org/en/latest/cookies.html


由于去年做的一个动态爬取url的项目,利用splinter向phantomjs中增加cookies都是没有问题的,当移植

到kail上面的时候就出现了如下异常。各种方式排出,还是没有发现问题的所在。

猜测可能是官方更新了第三方的库导致异常的发生,可能更改了接收和操作cookie的方式改变了,对比下

包的源码并未发现对cookie的操作进行了修改,都使用了最新的包原来的依然正确,可是kail下还是出现

异常。

于是就定位此路径下的webdriver.py文件。

C:\Python27\lib\site-packages\selenium-2.52.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py
定位到出错的信息

def execute(self, driver_command, params=None):
        """
        Sends a command to be executed by a command.CommandExecutor.


        :Args:
         - driver_command: The name of the command to execute as a string.
         - params: A dictionary of named parameters to send with the command.


        :Returns:
          The command's JSON response loaded into a dictionary object.
        """
        if self.session_id is not None:
            if not params:
                params = {'sessionId': self.session_id}
            elif 'sessionId' not in params:
                params['sessionId'] = self.session_id


        params = self._wrap_value(params)
        response = self.command_executor.execute(driver_command, params)
        print response #看看response输出的内容
        if response:
            self.error_handler.check_response(response)
            response['value'] = self._unwrap_value(
                response.get('value', None))
            return response
        # If the server doesn't send a response, assume the command was
        # a success
        return {'success': 0, 'value': None, 'sessionId': self.session_id}
于是就打印response信息,看看导致正确和错误的有何不同,输出的reponse信息

{u'status': 0, u'sessionId': u'85fa2740-ea82-11e5-b543-ef7d085a312d', u'value': {u'rotatab
le': False, u'takesScreenshot': True, u'acceptSslCerts': False, u'browserConnectionEnabled
': False, u'javascriptEnabled': True, u'driverVersion': u'1.2.0', u'databaseEnabled': Fals
e, u'locationContextEnabled': False, u'phantomjs.page.settings.loadImages': False, u'platf
orm': u'windows-7-32bit', u'browserName': u'phantomjs', u'version': u'2.0.0', u'driverName
': u'ghostdriver', u'nativeEvents': True, u'applicationCacheEnabled': False, u'webStorageE
nabled': False, u'proxy': {u'proxyType': u'direct'}, u'handlesAlerts': False, u'cssSelecto
rsEnabled': True}}

,发现了phantomjs的version,正确的版本为2.0.0,出现异常的为2.1.0,下载2.0.0版本的phantomjs

到kail上测试,发现可以成功添加coookie了,猜测主要原因可能是phantomjs的2.0以后的版本对cookie接收和操作的

方式做了修改。

在github提出问题是splinter一个bug,等待修复吧。

https://github.com/cobrateam/splinter/issues/478

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值