概要:

1、正常登录,使用selenium获取cookie;

2、保存cookie;

3、使用cookie登录。

4、python--2.7,selenium--3.4.1

步骤1

正常登录,使用selenium获取cookie。

代码如下。

handlingcookie.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/8/9 14:07
from selenium import webdriver if __name__ == '__main__':
url = "http://demo.avactis.com/4.7.9/sign-in.php"
username = "xxxxxxxxxx@gmail.com"
password = "xxxxxxxxxxxx"
id_username = "account_sign_in_form_email_id"
id_password = "account_sign_in_form_passwd_id"
css_button = ".btn.btn-primary.input_submit" driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_id(id_username).send_keys(username)
driver.find_element_by_id(id_password).send_keys(password)
driver.find_element_by_css_selector(css_button).click()
with open("cookie.data", "w") as f:
for cookie in driver.get_cookies():
print(cookie)
f.write(
str(cookie) + "\n"
)

执行后,保存cookie到文件

cookie.data

{u'domain': u'.avactis.com', u'secure': False, u'value': u'GA1.2.2145942479.1502271335', u'expiry': , u'path': u'/', u'httpOnly': False, u'name': u'_gid'}
{u'domain': u'.avactis.com', u'secure': False, u'value': u'GA1.2.1002502728.1502271335', u'expiry': , u'path': u'/', u'httpOnly': False, u'name': u'_ga'}
{u'domain': u'demo.avactis.com', u'secure': False, u'value': u'71a75b89b195945b4e9a80423711167e', u'expiry': 1510047349.50336, u'path': u'/', u'httpOnly': False, u'name': u'avactis_visitor_id'}
{u'domain': u'.avactis.com', u'secure': False, u'value': u'', u'expiry': , u'path': u'/', u'httpOnly': False, u'name': u'_gat'}
{u'domain': u'demo.avactis.com', u'secure': False, u'value': u'cmljk1spe04cdhlaefhtr3s4a0', u'expiry': 1504863349.503341, u'path': u'/', u'httpOnly': False, u'name': u'CZSESSID'}
{u'domain': u'demo.avactis.com', u'secure': False, u'value': u'save', u'expiry': 1502357746.463145, u'path': u'/', u'httpOnly': False, u'name': u'save_session'}

步骤2

使用cookie登录

loading_cookie_data.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/8/9 15:06
import time
from selenium import webdriver if __name__ == '__main__':
url = "http://demo.avactis.com/4.7.9" with open("cookie.data", "r") as f:
lis_lines = f.readlines() driver = webdriver.Chrome()
driver.get(url)
for line in lis_lines:
dic_line = eval(line.strip())
driver.add_cookie(dic_line)
driver.refresh()
time.sleep(5)

工程目录

需要注意

1、首先,打开url driver.get(url) ,然后才能添加cookie driver.add_cookie(dic_line) 。

2、handlingcookie.py中,登录后,不能点击页面上的signout,否则会导致cookie无效。

3、需要将chromedriver.exe的路径配置在环境变量中。

python selenium cookie 登录的更多相关文章

  1. Python Selenium Cookie 绕过验证码实现登录

    Python Selenium Cookie 绕过验证码实现登录 之前介绍过博客园的通过cookie 绕过验证码实现登录的方法.这里并不多余,会增加分析和另外一种方法实现登录. 1.思路介绍 1.1. ...

  2. 使用Python+Selenium模拟登录QQ空间

    使用Python+Selenium模拟登录QQ空间爬QQ空间之类的页面时大多需要进行登录,研究QQ登录规则的话,得分析大量Javascript的加密解密,这绝对能掉好几斤头发.而现在有了seleniu ...

  3. Python + Selenium 实现登录Office 365

    最近捡起之前用的Python + Selenium实现工作中需要的登录Office 365功能.(吐槽:国内网络真是卡,登录Office 365实属不易.另外Selenium这样的网站都要墙,无法理解 ...

  4. python+selenium自动化登录dnf11周年活动界面领取奖励登录部分采坑总结[1]

    背景: Dnf的周年庆活动之一,游戏在6月22日 06:00~6月23日 06:00之间登陆过游戏后可以于6月25日 16:00~7月04日 06:00领取奖励 目标:连续四天自动运行脚本,自动领取所 ...

  5. selenium cookie 登录

    前言 爬虫方向的小伙伴们都知道网页爬虫经常遇到的问题就是登录账户,有些简单的网站我们可以简单的send key来输入账户密码就可以登录,但是有很多网站需要验证码之类的就不太好用了,这时候就体现到了co ...

  6. python+selenium实现登录账户

    selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(Selenium Gr ...

  7. Python+ Selenium自动化登录腾讯QQ邮箱实例

    学习了Python语言一段时间后,在公司的项目里也使用到了python来写测试脚本,一些重复的操作都使用脚本来处理了.大大的提高工作效率,减少了一些手工重复的操作. 以下是使用unittest框架写的 ...

  8. python selenium模拟登录163邮箱和QQ空间

    最近在看python网络爬虫,于是我想自己写一个邮箱和QQ空间的自动登录的小程序, 下面以登录163邮箱和QQ空间和为例: 了解到在Web应用中经常会遇到frame/iframe 表单嵌套页面的应用, ...

  9. 九、Python+Selenium模拟登录

    研究QQ登录规则的话,得分析大量Javascript的加密解密,比较耗时间.自己也是练习很少,短时间成功不了.所以走了个捷径. Selenium是一个WEB自动化测试工具,它运行时会直接实例化出一个浏 ...

随机推荐

  1. 51nod 125乘法逆元 (扩展欧几里得)

    给出2个数M和N(M < N),且M与N互质.找出一个数K满足0 < K < N且K * M % N = 1,假设有多个满足条件的.输出最小的. Input 输入2个数M, N中间用 ...

  2. ubuntu12.04 下安装nodejs

    liunx里面安装nodejs我也找了非常多文章,貌似对非常多liunx新手来讲不是非常清楚,以下是我结合一些文章,亲自实践得到的安装步骤.同一时候还有大家关心的与seajs紧密相关的spm模块的安装 ...

  3. mybatis like写法

    name like concat(concat('%',#{name}),'%') name like concat('%',#{name},'%')

  4. STL学习笔记(迭代器相关辅助函数)

    advance()可令迭代器前进 #include <iterator> void advance(InputIterator& pos,Dist n); 面对Random Acc ...

  5. squid cache 服务器端的安装,配置

    一,什么squid Squid是一个高性能的代理缓存服务器,可以加快内部网浏览Internet的速度,提高客户机的访问命中率.Squid不仅支持HTTP协议, 还支持FTP.gopher.SSL和WA ...

  6. webservice中DateTime类型參数的传入问题

    This step-by-step article describes how to format DateTime and Date values in the XML that is extrac ...

  7. Nginx + uwsgi 时,替换路径。

    { # Django api use uwsgi location /api/ { # /api/hello ==> /hello rewrite ^/api/(.*) /$1 break; # ...

  8. SpringBoot Idea 启动报错 Process finished with exit code 1

    问题描述:没有其他任何错误日志,只有Process finished with exit code 1 问题原因:Maven POM.xml问题造成 由于是properties是我直接从其他项目中拷贝 ...

  9. Android 你可能忽略的提高敲代码效率的方式 (转)

    每日推荐 Eyepetizer-in-Kotlin:一款简约的小视频app,带你走进kotlin 作为学习kotlin的一款app,在撸代码的过程中学习kotlin的语法及特性. Eyepetizer ...

  10. 要胀爆的Angular1.0

    尝试从http请求上遏制缓存: http://blog.csdn.net/u010039979/article/details/54376856 if (!$httpProvider.defaults ...