selenium操作浏览器cookies
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File; import java.io.FileReader;
import java.io.FileWriter;
import java.util.Date; import java.util.StringTokenizer; import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import common.StartChrome; public class TestCookies { public static void main(String[] args) { TestCookies.getCookies();
TestCookies.addCookies(); }
public static void addCookies(){
BufferedReader bufferedReader;
// WebDriver driver = StartFireFox.start();
WebDriver driver=StartChrome.start();
driver.get("http://www.zhihu.com/"); try {
File cookieFile = new File("zhizhu.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()).booleanValue(); Cookie cookie = new Cookie(name,
value,
domain,
path,
expiry,
isSecure); driver.manage().addCookie(cookie); }
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(); driver.get("http://www.zhihu.com");
} public static void getCookies(){ //WebDriver driver = StartFireFox.start();
WebDriver driver=StartChrome.start();
driver.get("https://www.zhihu.com/#signin");
//填写用户信息,然后登陆 driver.findElement(By.name("account")).sendKeys("XXXX");
driver.findElement(By.name("password")).sendKeys("XXXX");
if(driver.findElement(By.name("remember_me")).isSelected()){
driver.findElement(By.name("remember_me")).click(); }
driver.findElement(By.className("sign-button")).click(); try{
Thread.sleep(3000);
File cookieFile = new File("zhizhu.txt");
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.getValue()+";"
+ cookie.getDomain() + ";"
+ cookie.getPath() + ";"
+ cookie.getExpiry() + ";"
+ cookie.isSecure())); bufferedWriter.newLine(); }
bufferedWriter.flush();
bufferedWriter.close();
fileWriter.close(); }catch (Exception ex){ ex.printStackTrace(); }
System.out.println("close brower");
driver.quit();
} }
重点保存cookies到一个文件,然后每次读取文件
selenium操作浏览器cookies的更多相关文章
- [Selenium] 操作浏览器 Cookies
WebDriver 提供了一系列 Cookies 的操作来获取.填写.删除 Cookies 的方法,节省了多次在登陆页面的查找元素并填写登录信息的时间. 1)获取 Cookies ,并保存到文件中以备 ...
- [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的登录界面: ② 填写用户名和密码: ...
随机推荐
- Mac App开发
1. icns制作 在线工具: https://iconverticons.com/online/ 2. 替换dmg图标 选中dmg文件 右键, 选择显示简介 将icns图表拖拽到简介弹出框的左上角图 ...
- yagmail 实现发邮件
yagmail 实现发邮件 yagmail 可以更简单的来实现自动发邮件功能. github项目地址: https://github.com/kootenpv/yagmail 安装 pip insta ...
- Windows Internals 笔记——作业
1.Windows提供了一个作业内核对象,它允许我们将进程组合在一起并创建一个“沙箱”来限制进程能够做什么.创建只包含一个进程的作业同样非常有用,因为这样可以对进程施加平时不能施加的限制. 2.如果进 ...
- vmware安装centOs操作系统配置网络的一系列问题
1:最近公司在测试项目,需要在linux操作系统上面测试,可惜自己之前学linux操作系统不是很深,配置网络也不是很熟练,网上方法太多,但是不是很好用,确实难为了自己一把,在这里自己总结一下配置网络的 ...
- spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available
我也是最近开始学习Spring Boot,在执行异步操作的时候总是汇报如下的错误: Exception in thread "main" org.springframework.b ...
- js分析 猫_眼_电_影 字体文件 @font-face
0. 参考 https://developer.mozilla.org/zh-CN/docs/Web/CSS/@font-face 这是一个叫做@font-face 的CSS @规则 ,它允许网页开发 ...
- Java_打印从一个数到另一个数之间的整数,并每5个为一行
import java.util.Scanner; public class Dayin_100 { public static void main(String[] args) { System.o ...
- 2018-2019-1 20189201 《LInux内核原理与分析》第六周作业
希望是美好的 但仅仅是所想 一.书本第五章知识总结[系统调用的三层机制(下)] 深入理解系统调用的过程 system_call并不是一个普通的函数,只是一段汇编代码的起点,且内部没有严格遵守函数调用堆 ...
- phpstorm设置篇
1.设置全局字体编码: File->settings->Editor->File Encodings 进入这个页面后,有个Global Encoding , 默认是 UTF8 ,如果 ...
- centos+git+gitolite 安装和部署
一.部署环境 系统:CentOS 6.4x64 最小化安装 IP:192.168.52.131 git默认使用SSH协议,在服务器上基本上不用怎么配置就能直接使用.但是如果面向团队服务,需要控制权限的 ...