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 ...
随机推荐
- Luogu P2613 【模板】有理数取余
题目链接 \(Click\) \(Here\) 真心没啥东西,只要能\(Get\)到在数字输入的时候按位取模,以及除数也可以直接取模就可以了.(把每个数看做乘法原理和加法原理构造起来的即可.) #in ...
- qml: C++调用qml函数
C++调用qml函数,是通过下面的函数实现的: bool QMetaObject::invokeMethod(QObject *obj, const char *member, Qt::Connect ...
- CentOS 6.x 最小化安装推荐安装的依赖包和修改内核参数
CentOS 6.x 最小化安装推荐安装的依赖包 我在日常工作中,新建的xenserver的虚拟机,CentOS release 6.9 (Final)操作系统,采用最小化安装,后续很多操作需要各种依 ...
- mysql存储过程造数据取一个基准用户的各种类型都取一条数据作为基准数据,循环插入
CREATE DEFINER=`user`@`%` PROCEDURE `create_upload_data`(in start_userid integer,in create_counter i ...
- 信用评分卡 (part 3of 7)
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- 使用Thumb
目录 使用Thumb title: 使用Thumb tags: ARM date: 2018-10-24 19:28:32 --- 使用Thumb C文件使用编译选择增加 -mthumb即可,修改ma ...
- Ubuntu编译Protobuf(Base tensorflow-12)报错
报错1: dogepool@ubuntu-server:~/dogecoin$ ./autogen.sh./autogen.sh: 50: ./autogen.sh: autoreconf: not ...
- Java NIO系列教程(七) selector原理 Epoll版的Selector
目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...
- Spring的RestTemplate
Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,并且实现了对象与json的序列化和反序列化,非常方便.RestTemplate并没有限定Http的客户端类型 ...
- win7安装linux CentOS7双系统实践
开发需求要安装linux,百度了些资料,当然仅供参考,否则入坑. 步骤一 :制作Centos 7镜像文件,这步没什么坑 1.准备U盘8G以上 下载的话网上很多,这里提供一个下载路径: http:// ...