phantomJS报错:Can only set Cookies for the current domain

需求

有时候一些场景不得不使用phantomJS来获取页面动态加载的信息,但是遇到一些需要登录状态的页面,绕不过登录,就是能将cookies放入phantomJS中才行了

错误代码

# 获取浏览器
driver = webdriver.PhantomJS(executable_path='phantomjs.exe')
# 拿到cookies信息
cookie = {"SSOLoginState":"1488702265"}
    for key in cookie:
        c = {}
        c['name'] = key
        c['value'] = cookie[key]
        c['domain'] = 'example.com'
        c['page'] = '/'
        c['httponly'] = False
        c['secure'] = False
        driver.add_cookie(c)

报错信息

Can only set Cookies for the current domain...
....
...
Screenshot: available via screen

大概是说不能将cookies放入本域名,可能是跨域了

官网的例子

phantomJS官网
完整的cookies信息应该是这样的:

{ 
  domain: 'example.com',
  expires: 'Sat Oct 11 2014 21:44:33 GMT+0200 (CEST)',
  expiry: 1476128618,
  httponly: false,
  name: 'cookieName',
  path: '/',
  secure: false,
  value: cookieValue
}

原因:

原来cookies中有一个key是path,而不是page!!!!另外还有domain前面 需要加一个. 例如jianshu.com,则正确的domain应该写成

domain: '.jianshu.com'

你可能感兴趣的:(phantomJS报错:Can only set Cookies for the current domain)