selenium如何操作cookies实现免登录
执行接口测试或者某些自动化测试时,为了避免每次访问接口都需要登录操作,可以用访问接口时,把cookies信息传过去。
思路是先登录一次页面,获取到cookies信息,把cookies信息保存到本地文件,以后再访问页面时直接拿本地的cookies文件传过去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | package com.gmsd; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Set; import java.util.StringTokenizer; import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; /** * @author QiaoJiaofei * @version 创建时间:2015年7月2日 上午10:29:54 * 类说明 */ public class TestCookie { WebDriver dr = null ; public static void main(String args[]) { TestCookie t = new TestCookie(); t.wtriteC(); t.getC(); } public void wtriteC() { System.setProperty( "webdriver.chrome.driver" , "D:/BaiduYunDownload/selenium/chromedriver.exe" ); dr = new ChromeDriver(); dr.get( "http://172.16.30.209:5555/login.shtml" ); dr.findElement(By.id( "inputEmail" )).sendKeys( "18600363820" ); dr.findElement(By.id( "inputPassword" )).sendKeys( "123456" ); dr.findElement(By.id( "denglu" )).click(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); File file = new File( "D:/JavaTestFile/TestCookies.txt" ); Set<Cookie> s = dr.manage().getCookies(); if (file.exists()) { file.delete(); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); for (Cookie c:s) { bw.write(c.getDomain()+ ";" +c.getName()+ ";" +c.getValue()+ ";" +c.getPath()+ ";" +c.getExpiry()+ ";" +c.isSecure()); bw.newLine(); } bw.flush(); bw.close(); fw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } dr.quit(); } public void getC() { System.setProperty( "webdriver.chrome.driver" , "D:/BaiduYunDownload/selenium/chromedriver.exe" ); dr = new ChromeDriver(); dr.get( "http://172.16.30.209:5555" ); File file = new File( "D:/JavaTestFile/TestCookies.txt" ); try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line = null ; while ((line=br.readLine())!= null ) { String [] sz = line.split( ";" ); String doman = sz[ 0 ].trim(); String name = sz[ 1 ].trim(); String value = sz[ 2 ].trim(); String path = sz[ 3 ].trim(); Date date = null ; if (!(sz[ 4 ].equals( "null" ))){ date = new Date(sz[ 4 ]); //System.out.println("date="+date); } Boolean bl = Boolean.valueOf(sz[ 5 ]); Cookie ck = new Cookie(name,value,doman,path,date,bl); dr.manage().addCookie(ck); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } dr.get( "http://172.16.30.209:5555" ); //dr.quit(); } } |
1 | writerC()方法是获取cookies并存到本地文件,主要使用selenium的Set<Cookie> s = dr.manage().getCookies(); |
1 | getC()方法是从本地文件读取cookies信息,主要使用 |
Cookie ck = new Cookie(name,value,doman,path,date,bl);
dr.manage().addCookie(ck);
注意几点:
1、Cookie的构造方法的参数是有顺序的
2、getC()调用了两次url,第一次是往url传cookies,第二次是为了刷新,也可使用dr.navigate().refresh();
******************************************************************************************************************************************
作者:乔叶叶
博客地址:http://www.cnblogs.com/qiaoyeye/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
******************************************************************************************************************************************
作者:乔叶叶
博客地址:http://www.cnblogs.com/qiaoyeye/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
******************************************************************************************************************************************
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?