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的更多相关文章

  1. selenium操作浏览器cookies

    package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; imp ...

  2. [Python爬虫]使用Selenium操作浏览器订购火车票

    这个专题主要说的是Python在爬虫方面的应用,包括爬取和处理部分 [Python爬虫]使用Python爬取动态网页-腾讯动漫(Selenium) [Python爬虫]使用Python爬取静态网页-斗 ...

  3. 每次用 selenium 操作浏览器都还原了 (比如没有浏览器历史记录)

    每次用 selenium 操作浏览器都还原了 (比如没有浏览器历史记录)

  4. selenium操作浏览器cookie方法

    /**     * 操作浏览器的cookie     */        @Test    public void testCookie()throws Exception{        drive ...

  5. selenium操作浏览器的前进和后退

    前进关键字:driver.forward() 后退关键字:driver.back() 测试对象:1.https://www.baidu.com/ 2.https://www.sogou.com/ 实例 ...

  6. 『心善渊』Selenium3.0基础 — 3、使用Selenium操作浏览器对象的基础API

    目录 1.导入Selenium库 2.创建浏览器对象 3.浏览器窗口大小设置 4.浏览器位置设置 5.请求访问网址 6.浏览器页面前进.后退和刷新 7.关闭浏览器 相比于高大上的各种Selenium进 ...

  7. 『心善渊』Selenium3.0基础 — 17、Selenium操作浏览器窗口的滚动条

    目录 1.为什么操作滚动条 2.Selenium如何操作滚动条 3.Selenium操作滚动条方法 4.操作滚动条示例 5.下拉至聚焦元素的位置 (1)实现步骤: (2)实现示例: 1.为什么操作滚动 ...

  8. 『心善渊』Selenium3.0基础 — 18、使用Selenium操作浏览器的弹窗

    目录 1.操作浏览器自带弹窗 2.操作浏览器页面自定义弹窗 1.操作浏览器自带弹窗 (1)说明: webdriver中处理JavaScript所生成的alert.confirm 以及prompt 弹窗 ...

  9. Selenium+java操作浏览器cookies

    描述:登录CSDN,将登录信息cookies保存到文件,再次打开网页时,直接利用文件中的数据登录. 1. 获取cookies并保存到文件 步骤: ① 打开CSDN的登录界面: ② 填写用户名和密码: ...

随机推荐

  1. android Paint属性

    **       * Paint类介绍       *        * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色,     * 样式等绘制信息,指定了如何绘制文本和图形 ...

  2. 转载免费的SSL证书

    目前我知道的有2种方式进行免费的SSL证书的获取 第一种:腾讯云申请 第二种:Let's Encrypt (国外在) 我一直使用第一种,还可以,有效期1年. 以下转载第二种: 实战申请Let's En ...

  3. 记一次ORM的权衡和取舍

    面对ORM的选型,有些人是根据自己熟悉程度来评判,有些人是根据他人的推荐来抉择,有些人觉得都差不多,随便了.当自己要真正做选择的时候,以上的这些依据都无法真正说服自己,因为不同的业务需求,不同的团队构 ...

  4. Codeforces 12 D Ball

    Discription N ladies attend the ball in the King's palace. Every lady can be described with three va ...

  5. java队列--queue详细分析

    ---恢复内容开始--- Queue:基本上一个队列就是一个先入先出(FIFO)的数据结构 Queue接口与List.Set同一级别,都是继承了Collection接口,LinkedList实现了Li ...

  6. Mac电脑解压文件unrar用密码问题解决

    下载了一个rar文件,有密码的,你懂的. 但是在mac上面,用unrar解压,只能解出空文件:用izip解压,直接停在那里不动. 只好上网搜索.找到了办法. 用brew 安装了命令行版本的 unrar ...

  7. 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

    题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...

  8. Katalon

    Katalon---一款好用的selenium自动化测试插件 selenium框架是目前使用较广泛的开源自动化框架,一款好的.基于界面的录制工具对于初学者来说可以快速入门:对于老手来说可以提高开发自动 ...

  9. 从头学起-CLR的执行模型

    1.将源代码编译成托管代码 公共运行时(Common Language Runtime) a.面向运行时的所有语言都可以通过异常报告错误 b.面向运行时的所有语言都可以创建线程 c.核心功能:管理内存 ...

  10. JOIN ,LEFT JOIN ,ALL JOIN 等的区别和联系

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录  right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner join(等值连接) ...