selenium修改cookie
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、获取线上环境代码
1 from multiprocessing import Process 2 from pageobjects.production_login import Product_Login 3 from config.cfg import * 4 import time 5 6 class Test_Login: 7 '''登录测试环境''' 8 def __init__(self,driver): 9 self.driver = driver 10 11 def open_browser(self): 12 self.driver.maximize_window() 13 self.driver.implicitly_wait(10) # 等待 14 15 def login(self): 16 '''登录测试环境,添加线上Token''' 17 self.driver.get(test_url) 18 self.driver.find_element_by_id("vcc_code").send_keys(vcc[1]) 19 self.driver.find_element_by_id("username").send_keys(username[1]) 20 self.driver.find_element_by_id("password").send_keys(password) 21 self.driver.find_element_by_id('btn').click() 22 self.driver.delete_cookie('access_token') # 删除测试环境的token 23 24 def add_cookie(self,product): 25 '''读取线上token,写入cookie''' 26 new_token = product.readtoken()[0] 27 self.driver.add_cookie(new_token) 28 29 def login_test(self): 30 product = Product_Login() 31 lastTime = product.readtoken()[1] #获取日期 32 if (time.strftime("%d")) == lastTime: #如果日期是今天,则直接取token 33 self.login() 34 self.add_cookie(product) 35 else: 36 p = Process(target=product.login_production) #运行线上环境进程 37 p.start() 38 self.login() 39 p.join() #等待 40 self.add_cookie(product) 41 42 def close_browser(self): 43 self.driver.quit()
2、测试环境使用token
1 import time , unittest,json 2 from selenium import webdriver 3 from selenium.webdriver.chrome.options import Options 4 from config.cfg import * 5 6 7 8 class Product_Login: 9 '''登录线上环境,把获取的token写入配置文件''' 10 11 def login_production(self): 12 '''selenium+chromeheadless获取线上环境Token''' 13 day = time.strftime("%d") 14 15 chrome_options = Options() 16 chrome_options.add_argument('--headless') 17 driver = webdriver.Chrome(options=chrome_options) 18 driver.implicitly_wait(10) # 等待 23 driver.get(production_url) 24 driver.find_element_by_id("vcc_code").send_keys(vcc[0]) # 登录 25 driver.find_element_by_id("username").send_keys(username[0]) 26 driver.find_element_by_id("password").send_keys(password) 27 driver.find_element_by_id('btn').click() 28 29 manage_cookie = driver.get_cookie('access_token') 30 driver.quit() 31 manage_cookie = json.dumps(man_cookie) 32 self.writetoken(day,man_cookie) 33 34 def writetoken(self, nowTime,manage_cookie): # 写线上token 35 36 out = open(r'D:\config.txt', 'w') 37 out.writelines(man_cookie) #写入token 38 out.writelines("\n") 39 out.writelines(nowTime) #写入日期 40 out.writelines("\n") 41 out.close() 42 43 def readtoken(self): # 读token 44 45 out = open(r'D:\config.txt', encoding='utf-8') 46 lines = out.readline() #token 47 if (lines.__contains__("\n")): 48 lines = lines.split("\n")[0] 49 lines = json.loads(lines) 50 lines1 = out.readline() #日期 51 if (lines1.__contains__("\n")): 52 lines1 = lines1.split("\n")[0] 53 54 out.close() 55 return lines,lines1 56