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 ...
随机推荐
- iostat 磁盘io分析工具
一:简介 iostat(I/O statistics)输入输出缩写,用来动态监视系统的磁盘操作活动.它能监视磁盘的活动统计情况,同时也能监视CPU的活动情况.缺点是,iostat不能对某一个具体的进程 ...
- u-boot(三)启动文件
目录 u-boot(三)启动文件 汇编 C:_start_armboot 代码摘要 C:main_loop 内核启动 菜单处理(自定义实现) 命令处理 title: u-boot(三)启动文件 tag ...
- nGrinder TestRunnerInsertMysqlSingle.groovy
s C:\Users\Lindows\workspace\GroovyTest\src\com\iteye\lindows\mysql\TestRunnerInsertMysqlSingle.groo ...
- Integer与int值的比较
==一般用于比较内存地址,equals()用于比较Object的值,注意int用equals()是会报错的.Integer i=1Integer k=1i.equals(k)=truei==k=tru ...
- redis使用问题一:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause
本文使用的是spring-data-redis 首先说下redis最简单得使用,除去配置. 需要在你要使用得缓存得地方,例如mybatis在mapper.xml中加入: <cache evict ...
- http-request详解
HTTP请求 请求数据格式 响应数据格式 request
- golang使用redis
redigo使用 手册地址:http://godoc.org/github.com/garyburd/redigo/redis github地址:https://github.com/garyburd ...
- windows10下TensorFlow安装记录
1.安装anaconda 安装最新版:https://repo.anaconda.com/archive/Anaconda3-5.3.0-Windows-x86_64.exe 加入环境变量: path ...
- Java入门系列 泛型
前言 <Java编程思想>第四版足足用了75页来讲泛型——厚厚的一沓内容,很容易让人头大——但其实根本不用这么多,只需要一句话:我是一个泛型队列,狗可以站进来,猫也可以站进来,但最好不要既 ...
- Nginx不支持Post数据中带有"."
如果提交的数据中带有.,nginx会把数据过滤掉,会导致后台收不到数据.下面是一个错误示例及解决办法