[Selenium] 操作浏览器 Cookies
WebDriver 提供了一系列 Cookies 的操作来获取、填写、删除 Cookies 的方法,节省了多次在登陆页面的查找元素并填写登录信息的时间。
1)获取 Cookies ,并保存到文件中以备后续使用:
package com.learningselenium.normalwebdriver;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.Date;
import java.util.StringTokenizer;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Webdriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class testGetCookies{
public static void main(String... args){
WebDriver driver = new FirefoxDriver();
driver.get("http://zhizhu.com/#signin");
//填写用户信息,然后登陆
driver.findElement(By.name("email")).sendKeys("seleniumcookies@126.com");
driver.findElement(By.name("password").sendKeys("cookies123"));
if(driver.findElement(By.name("rememberme")).isSelected()){
driver.findElement(By.name("remermberme")).click();
}
driver.findElement(By.className("sign-button")).click();
File cookieFile = new File("zhizhu.cookie.txt");
try{
cookieFile.delete();
cookieFile.createNewFile();
FileWriter fileWriter = new FileWriter(cookieFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Cookie cookie : driver.manage().getCookies()){
bufferedWriter.write((cookie.getName() + ";" + cookie.getDomain() + ";" + cookie.getExpiry() + ";" + cookie.isSecure()));
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedWriter.close();
fileWriter.close();
}catch (Exception ex){
ex.printStackTrace();
}
driver.quit();
}
}
2)读取保存的Cookies 信息并自动填充到新打开的浏览器 Cookies 中,然后直接进入登陆状态后的页面
public class testAddCookies{
private static BufferedReader bufferedReader;
public static void main(String... args){
WebDriver driver = new FirefoxDriver();
driver.get("http://zhizhu.com/#signin");
try{
File cookieFile = new File("zhihu.cookie.txt");
FileReader fr = new FileReader(cookieFile);
bufferedReader = new BufferedReader(fr);
String line;
while((line = bufferedReader.readLine()) != null){
StringTokenizer stringTokenizer = new StringTokenizer(line, ";");
while (stringTokenizer.hasMoreTokens()){
String name = stringTokenizer.nextToken();
String value= stringTokenizer.nextToken();
String domain = stringTokenizer.nextToken();
String path = stringTokenizer.nextToken();
Date expiry = null;
String dt;
if(! (dt = stringTokenizer.nextToken()).equals("null")){
expiry = new Date(dt);
}
boolean isSecure = new Boolean(stringTokenizer.nextToken()).booleanVaue();
Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
driver.get("http://www.zhihu.com");
}
[Selenium] 操作浏览器 Cookies的更多相关文章
- selenium操作浏览器cookies
package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; imp ...
- [Python爬虫]使用Selenium操作浏览器订购火车票
这个专题主要说的是Python在爬虫方面的应用,包括爬取和处理部分 [Python爬虫]使用Python爬取动态网页-腾讯动漫(Selenium) [Python爬虫]使用Python爬取静态网页-斗 ...
- 每次用 selenium 操作浏览器都还原了 (比如没有浏览器历史记录)
每次用 selenium 操作浏览器都还原了 (比如没有浏览器历史记录)
- selenium操作浏览器cookie方法
/** * 操作浏览器的cookie */ @Test public void testCookie()throws Exception{ drive ...
- selenium操作浏览器的前进和后退
前进关键字:driver.forward() 后退关键字:driver.back() 测试对象:1.https://www.baidu.com/ 2.https://www.sogou.com/ 实例 ...
- 『心善渊』Selenium3.0基础 — 3、使用Selenium操作浏览器对象的基础API
目录 1.导入Selenium库 2.创建浏览器对象 3.浏览器窗口大小设置 4.浏览器位置设置 5.请求访问网址 6.浏览器页面前进.后退和刷新 7.关闭浏览器 相比于高大上的各种Selenium进 ...
- 『心善渊』Selenium3.0基础 — 17、Selenium操作浏览器窗口的滚动条
目录 1.为什么操作滚动条 2.Selenium如何操作滚动条 3.Selenium操作滚动条方法 4.操作滚动条示例 5.下拉至聚焦元素的位置 (1)实现步骤: (2)实现示例: 1.为什么操作滚动 ...
- 『心善渊』Selenium3.0基础 — 18、使用Selenium操作浏览器的弹窗
目录 1.操作浏览器自带弹窗 2.操作浏览器页面自定义弹窗 1.操作浏览器自带弹窗 (1)说明: webdriver中处理JavaScript所生成的alert.confirm 以及prompt 弹窗 ...
- Selenium+java操作浏览器cookies
描述:登录CSDN,将登录信息cookies保存到文件,再次打开网页时,直接利用文件中的数据登录. 1. 获取cookies并保存到文件 步骤: ① 打开CSDN的登录界面: ② 填写用户名和密码: ...
随机推荐
- 快速掌握RabbitMQ(四)——两种消费模式和QOS的C#实现
本篇介绍一下RabbitMQ中的消费模式,在前边的所有栗子中我们采用的消费者都是EventingBasicConsumer,其实RabbitMQ中还有其他两种消费模式:BasicGet和QueueBa ...
- ansible、zabbix、tcpdump
Ansible 源码安装 https://blog.csdn.net/williamfan21c/article/details/53439307 Ansible安装过程中常遇到的错误 http:// ...
- Python知识图谱
一.Python全栈图谱 2.Python语言高级 Python 全栈工程师前端 Python全栈工程师后端 Python Linux运维自动化开发 Python KaliLinux信息安全开发和使用 ...
- [转] 一句shell命令搞定代码行数统计
今天面试时,突然被面试官问到怎样用shell命令搞定某个文件夹下java代码行数的统计. 想了一下,基本思路就是找到这个文件夹下面的所有java文件,然后每个文件统计一下代码,外层套个for循环,叠加 ...
- 基于canvas和Web Audio的音频播放器
wavesurfer.js是一款基于HTML5 canvas和Web Audio的音频播放器插件.通过wavesurfer.js你能够使用它来制作各种HTML5音频播放器,它能够在各种支持 Web A ...
- 让Quality Center走下神坛--测试管理工具大PK(转)
让Quality Center走下神坛--测试管理工具QC/ALM 和 RQM.Jira.TP.SCTM大PK 在写完了<让QTP走下神坛>之后,现在来谈谈测试管理工具,献给所有正在或打算 ...
- ok6410[000] ubuntu1604_64bit下安装wps
虽说Ubuntu下有自动的office工具,不过使用上体验很差.而国内最好的office软件也就是金山的wps. ------------------------------------------- ...
- 对交换机VLAN及各种端口类型的理解
每学习一种技术时,我们往往需要去了解why,即这个技术是为解决什么问题而出现的. VLAN全称为Virtual Local Area Network,即虚拟局域网,是逻辑上的一种划分.一般来说,如果交 ...
- LINQ体验(18)——LINQ to SQL语句之视图和继承支持
视图 我们使用视图和使用数据表类似,仅仅需将视图从"server资源管理器/数据库资源管理器"拖动到O/R 设计器上,自己主动能够创建基于这些视图的实体类.我们能够同操作数据表一样 ...
- poj 3585 Accumulation Degree(二次扫描和换根法)
Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...