selenium添加cookie报错Message: unabl
2018-07-10 本文已影响9人
sexy_cyber
给selenium添加cookie总是报错:unable to set cookie
解决方案:
三步走:
1.请求目标站点;
2.删除旧的cookie,添加目标cookie;
3.再次请求目标站点;
按照上述流程便可成功,代码如下,注意add_cookie()传递的数据结构,和参数关键字
cookie = [{'name': 'pt2gguin', 'value': 'o0654921690'}, {'name': 'RK', 'value': 'mSglPLOFQ8'}, {'name': 'ptcz', 'value': '5eb1aeb628b6ab67aa306285a78434959b385f8e39415bda4ff4db3ea7763d75'}, {'name': 'pgv_pvid', 'value': '116706325'}, {'name': 'pgv_pvi', 'value': '1972684800'}, {'name': 'pgv_si', 'value': 's3116448768'}, {'name': 'pgv_info', 'value': 'ssid'}, {'name': 'uin', 'value': 'o0654921690'}, {'name': 'skey', 'value': '@7pMFazBu1'}, {'name': 'ptisp', 'value': 'ctc'}, {'name': 'ts_refer', 'value': 'www.baidu.com/link'}, {'name': 'ts_uid', 'value': '3500005680'}, {'name': 'userid', 'value': '5661917'}]
def upload():
url = 'https://om.qq.com/article/articlePublish#/!/view:article?typeName=multivideos'
driver.get(url)
driver.delete_all_cookies()
for c in cookie:
new = dict(c,**{
"domain": ".qq.com",
"expires": "",
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False,
})
driver.add_cookie(new)
driver.get(url)
path = 'xx.png'
driver.save_screenshot(path)
这是登陆成功后保存在本地的图片