Selenium: Trying to log in with cookies and get the errorMessage - “Can only set cookies for current domain” or "Unable to set Cookie"
from selenium import webdriver driver = webdriver.PhantomJS(executable_path='G:/OpenSources/phantomjs-2.1.1-windows/bin/phantomjs')
driver.get("http://pythonscraping.com")
driver.implicitly_wait(1)
print(driver.get_cookies()) savedCookies = driver.get_cookies()
driver2 = webdriver.PhantomJS(executable_path='G:/OpenSources/phantomjs-2.1.1-windows/bin/phantomjs')
driver2.get("http://pythonscraping.com")
driver2.delete_all_cookies()
for cookie in savedCookies:
driver2.add_cookie(cookie) driver2.get("http://pythonscraping.com")
driver2.implicitly_wait(1)
print(driver2.get_cookies())
以上代码15行报错: "errorMessage":"Can only set Cookies for the current domain"
将15行代码改为:
driver2.add_cookie({k: cookie[k] for k in ('name', 'value', 'domain', 'path', 'expiry') if k in cookie})
修改后报错: "errorMessage":"Unable to set Cookie"
最终修改:
from selenium import webdriver
import time driver = webdriver.PhantomJS(executable_path='G:/OpenSources/phantomjs-2.1.1-windows/bin/phantomjs')
driver.get("http://pythonscraping.com")
driver.implicitly_wait(1)
print(driver.get_cookies()) savedCookies = driver.get_cookies()
driver2 = webdriver.PhantomJS(executable_path='G:/OpenSources/phantomjs-2.1.1-windows/bin/phantomjs')
driver2.get("http://pythonscraping.com")
driver2.delete_all_cookies()
for cookie in savedCookies:
# fix the problem-> "errorMessage":"Unable to set Cookie"
for k in ('name', 'value', 'domain', 'path', 'expiry'):
if k not in list(cookie.keys()):
if k == 'expiry':
t = time.time()
cookie[k] = int(t) # 时间戳 秒
# fix the problem-> "errorMessage":"Can only set Cookies for the current domain"
driver2.add_cookie({k: cookie[k] for k in ('name', 'value', 'domain', 'path', 'expiry') if k in cookie}) driver2.get("http://pythonscraping.com")
driver2.implicitly_wait(1)
print(driver2.get_cookies())
在这个例子中,第一个 webdriver获得了一个网站,打印 cookie 并把它们保存到变量savedCookies里。第二个webdriver加载同一个网站(技术提示:必须首先加载网站,这样Selenium 才能知道cookie 属于哪个网站,即使加载网站的行为对我们没任何用处),删除所有的cookie ,然后替换成第一个webdriver得到的cookie 。当再次加载这个页面时,两组cookie 的时间戳、源代码和其他信息应该完全一致。
Selenium: Trying to log in with cookies and get the errorMessage - “Can only set cookies for current domain” or "Unable to set Cookie"的更多相关文章
- selenium IDE中log的保存与查看方法
下载selenium IDE的log保存插件为File Logging(selenium IDE),下载安装方式如下: (1)打开firefox浏览器----点击右上角----附加组件---插件--- ...
- scrapy cookies:将cookies保存到文件以及从文件加载cookies
我在使用scrapy模拟登录新浪微博时,想将登录成功后的cookies保存到本地,下次加载它实现直接登录,省去中间一系列的请求和POST等.关于如何从本次请求中获取并在下次请求中附带上cookies的 ...
- JS存取Cookies值,附自己写的获取cookies的一个方法
参考:脚本之家 这里对cookie进行了说明,也介绍了几个方法,但是我要取我存的cookie时取不到,他的方法只是针对存的 名字-值,不涉及键,所以自己写了个方法,来满足我的需求. ①你首先的了解C ...
- Can only set Cookies for the current domain
# -*- coding: utf-8 -*- """ Created on Mon Dec 12 14:35:49 2016 @author: yaru "& ...
- selenium webdriver 如何添加cookie
一. webdriver中常用的cookie方法 webdriver中提供了操作cookie的相关方法: get_cookies() 获得cookie信息 add_c ...
- selenium webdriver如何添加cookie
一. webdriver中常用的cookie方法 webdriver中提供了操作cookie的相关方法: get_cookies() 获得cookie信息 add_c ...
- bottle源码
import sys __author__ = 'Marcel Hellkamp' __version__ = '0.13-dev' __license__ = 'MIT' ############# ...
- [PHP] Phalcon操作示范
这篇内容将对下列操作进行示范: Insert.Select.Update.Calculation.Transaction.models advanced.dev-tools.cookies [ Ins ...
- selenium+requests进行cookies保存读取操作
看这篇文章之前大家可以先看下我的上一篇文章:cookies详解 本篇我们就针对上一篇来说一下cookies的基本应用 使用selenium模拟登陆百度 from selenium import web ...
随机推荐
- ttribute "xmlns" was already specified for element "web-app".
今天导入了,一个javaweb项目,当自己对项目进行重命名的时候时候,但是web.xml直接爆出来一个"ttribute "xmlns" was already spec ...
- Python3 图片转字符画
https://www.shiyanlou.com/courses/370/labs/1191/document from PIL import Image import argparse ascii ...
- .gitignore无效的原因
有时候,我们编写gitinore后发现文件还是没有被忽略,这是什么原因呢? 熟知git的老鸟们可能已经知道,因为这个文件在之前已经被追踪了,如果想忽略已经被追踪的文件我们需要把这个追踪去除. 对所有文 ...
- HDFS集群优化篇
HDFS集群优化篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.操作系统级别优化 1>.优化文件系统(推荐使用EXT4和XFS文件系统,相比较而言,更推荐后者,因为XF ...
- Jquery Pagination分页插件使用
JqueryPagination是个简单轻量级的分页插件,使用起来很容易,只要初始化一个实例,并设置总数量.翻页回调函数.其它参数就可以实现无刷新分页功能了. 准备工作 下载jquery.min.js ...
- java io系列11之 FilterOutputStream
FilterOutputStream 介绍 FilterOutputStream 的作用是用来“封装其它的输出流,并为它们提供额外的功能”.它主要包括BufferedOutputStream, Dat ...
- Dubbo优雅关机原理
Dubbo是通过JDK的ShutdownHook来完成优雅停机的 所以如果用户使用 kill -9 PID 等强制关闭命令,是不会执行优雅停机的 只有通过 kill PID时,才会执行 原理: · 服 ...
- 在.NET程序中实现HttpServer功能
亲爱的下午茶 博客园 首页 新随笔 联系 订阅 管理 随笔-6 文章-0 评论-10 在.NET程序中实现HttpServer功能 最近在实现一个可视化数据解析工具,需要在Wpf程序中实 ...
- spring boot @Value Could not resolve placeholder
@Configuration public class PropertySourcePlaceholderConfig { @Bean public PropertySourcesPlaceholde ...
- 059、安装配置flannel(2019-03-28 周四)
参考https://www.cnblogs.com/CloudMan6/p/7424858.html build flannel flannel 没哟现成的执行文件可用,必须自己build,最 ...