WebDriver操作cookie的方法:cookie以字典形式保存

  • get_cookies()     获取所有的cookie
  • get_cookie(name)   返回字典的key为"name"的cookie信息
  • add_cookie(cookie_dict)   添加字典形式的cookie
  • delete_cookie(name)   删除cookie
  • delete_alla-cookies():删除所有cookie信息

最近在写项目的登录功能时,需要把线上环境的token和测试环境的结合使用。所以需要对测试环境的cookie操作。

思路是:用selenium登录测试环境,同时也用进程在后台用headless方式登录线上环境。取到线上的token,再给测试环境用。

代码如下:

1、获取线上环境代码


 from multiprocessing import Process
from pageobjects.production_login import Product_Login
from config.cfg import *
import time class Test_Login:
'''登录测试环境'''
def __init__(self,driver):
self.driver = driver def open_browser(self):
self.driver.maximize_window()
self.driver.implicitly_wait(10) # 等待 def login(self):
'''登录测试环境,添加线上Token'''
self.driver.get(test_url)
self.driver.find_element_by_id("vcc_code").send_keys(vcc[1])
self.driver.find_element_by_id("username").send_keys(username[1])
self.driver.find_element_by_id("password").send_keys(password)
self.driver.find_element_by_id('btn').click()
self.driver.delete_cookie('access_token') # 删除测试环境的token def add_cookie(self,product):
'''读取线上token,写入cookie'''
new_token = product.readtoken()[0]
self.driver.add_cookie(new_token) def login_test(self):
product = Product_Login()
lastTime = product.readtoken()[1] #获取日期
if (time.strftime("%d")) == lastTime: #如果日期是今天,则直接取token
self.login()
self.add_cookie(product)
else:
p = Process(target=product.login_production) #运行线上环境进程
p.start()
self.login()
p.join() #等待
self.add_cookie(product) def close_browser(self):
self.driver.quit()

2、测试环境使用token


 import time , unittest,json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from config.cfg import * class Product_Login:
'''登录线上环境,把获取的token写入配置文件''' def login_production(self):
'''selenium+chromeheadless获取线上环境Token'''
day = time.strftime("%d") chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(10) # 等待
driver.get(production_url)
driver.find_element_by_id("vcc_code").send_keys(vcc[0]) # 登录
driver.find_element_by_id("username").send_keys(username[0])
driver.find_element_by_id("password").send_keys(password)
driver.find_element_by_id('btn').click() manage_cookie = driver.get_cookie('access_token')
driver.quit()
manage_cookie = json.dumps(man_cookie)
self.writetoken(day,man_cookie) def writetoken(self, nowTime,manage_cookie): # 写线上token out = open(r'D:\config.txt', 'w')
out.writelines(man_cookie) #写入token
out.writelines("\n")
out.writelines(nowTime) #写入日期
out.writelines("\n")
out.close() def readtoken(self): # 读token out = open(r'D:\config.txt', encoding='utf-8')
lines = out.readline() #token
if (lines.__contains__("\n")):
lines = lines.split("\n")[0]
lines = json.loads(lines)
lines1 = out.readline() #日期
if (lines1.__contains__("\n")):
lines1 = lines1.split("\n")[0] out.close()
return lines,lines1
 

selenium修改cookie的更多相关文章

  1. 『心善渊』Selenium3.0基础 — 20、Selenium对Cookie的操作

    目录 1.Cookie介绍 2.Session介绍 3.Cookie工作原理图解 4.Cookie内容参数说明 5.Selenium操作Cookie的API 6.Selenium操作Cookie的示例 ...

  2. 云计算之路-阿里云上:负载均衡错误修改Cookie造成用户无法登录

    最近陆续有用户反馈在我们网站上登录时遇到登录死循环问题.输入用户名与密码提交后,显示登录成功,但跳转到目标网址后(由于依然处于未登录状态)又跳转回登录页面,再次登录,再次这样...就这样一直循环,怎么 ...

  3. 设置cookie,获取cookie,删除cookie,修改cookie

    怎么设置cookie,怎么设置cookie以及删除cookie和cookie详解 在操作cookie之前,先来看一下cookie长什么样. 可以看到,cookie是一个个键值对(“键=值”的形式)加上 ...

  4. 通过google浏览器的开发者工具修改cookie值

    打开一个页面F12,然后再刷新下.点到如下图位置刚可以添加或重设cookie的值.修改cookie的值时,需要注意要清除下cookie.不然修改的cookie不生效.

  5. 谷歌浏览器修改cookie访问网页的小插件——EditsThisCookie

    cookie是服务器用来区分不同的浏览器客户端的,比如学生A和B同一时段用各自的电脑访问学校访问学校的教务系统查看成绩,登录之后,访问同一页面确出来不同的信息,并且不能查看对方的信息,这就是因为服务器 ...

  6. 在php中修改cookie值遇到的奇怪问题

    本想修改cookie的值比较简单,结果测试发现并不是. 刚开始实现cookie修改的思路:先删除以前的cookie值,再创建一个新的. setcookie('name',value,time()-1) ...

  7. 服务器无法在发送 HTTP 标头之后修改 cookie

    隔三差五就碰到VS报错: System.Web.HttpException:“服务器无法在发送 HTTP 标头之后修改 cookie.” 解决后过几天又忘记了. 原因是: 程序为每个页面在config ...

  8. python+selenium操作cookie

    WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除cookie信息. WebDriver操作cookie的方法: get_cookies(): 获得所有cookie信息. get ...

  9. 关于MVC中 服务器无法在发送 HTTP 标头之后修改 cookie此类问题的解决

    处理方法 使用过滤器控制权限时,若无权则跳转到无权页面,但是每次跳转都会出现 ERROR - System.Web.HttpException (0x80004005): 服务器无法在已发送 HTTP ...

随机推荐

  1. 在VM虚拟机中安装Centos操作系统

    首先我们要下载  Centos https://www.centos.org/ 这个是Centos官方 最新版本 7 https://www.centos.org/download/ 提供有 DVD安 ...

  2. 007-chrome插件系列

    1.Axure RP Extension for Chrome 2.Charset 3.CLEAN crxMouse Gestures 4.Google 翻译 5.JSONView 6.restlet

  3. 部署的docker image总是太大,怎么办?

    sudo docker images REPOSITORY                        TAG                 IMAGE ID            CREATED ...

  4. CJSON create.c

    #include <stdio.h> #include "cJSON.h" /* { "semantic": { "slots" ...

  5. SpringMVC和Struts2的区别及优势

    1.SpringMVC和Struts2的区别比较 1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方 ...

  6. C#中类成员的执行顺序

    先进行细分: 类的成员分为:字段.属性.方法.构造方法 成员的修饰符:静态成员.实例成员 层次结构:父类.子类 先不考虑继承关系,执行顺序为: 静态字段静态构造方法实例字段实例构造方法属性和方法是在调 ...

  7. Java stream的常见用法

    不讲原理,只说用法. 1,集合遍历 public class StreamTest { public static void main(String[] args) { //1 遍历 List< ...

  8. 【转】Powershell与jenkins集成部署的运用(powershell运用)

    powershell简介: 远程管理采用的一种新的通信协议,Web Services for Management,简称WS-MAN它通过http或者https进行工作,WS-WAN的实现主要基于一个 ...

  9. react-native 导入高德地图

    高德官网 : https://lbs.amap.com/ GitHub地址: https://github.com/qiuxiang/react-native-amap3d 安装的时候遇到错误:一般是 ...

  10. mac & ip

    mac 解决本地网络机器的通信 ip 解决不同网络间主机的通信