# GetCookie.java

package com.meicai.tms;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions; public class GetCookie { // TODO Auto-generated method stub
public static void main(String[] args) { WebDriver driver = new ChromeDriver();
driver.get("url/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); WebElement user = driver
.findElement(By.name("email"));//("//*[@id='pl_login_form']/div[2]/div[3]/div[1]/div/input"));
user.clear();
user.sendKeys("username");
WebElement password = driver.findElement(By
.name("password"));//xpath("//*[@id='pl_login_form']/div[2]/div[3]/div[2]/div/input"));
password.clear();
password.sendKeys("password"); WebElement yan =driver.findElement(By.name("code"));
yan.clear();
yan.sendKeys("9527");
/* WebElement submit = driver.findElement(By
.xpath("/html/body/div/div/div[2]/form/div[4]/div/div/button"));
submit.click();*/
Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).perform(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement dianyidian =driver.findElement(By.xpath("/html/body/div/div/div[2]/a[3]"));
System.out.println(dianyidian.getText());
dianyidian.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} File file = new File("D:\\TmsCookie.data");
try {
// delete file if exists
file.delete();
file.createNewFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (Cookie ck : driver.manage().getCookies()) {
bw.write(ck.getName() + ";" + ck.getValue() + ";"
+ ck.getDomain() + ";" + ck.getPath() + ";"
+ ck.getExpiry() + ";" + ck.isSecure());
bw.newLine();
}
bw.flush();
bw.close();
fw.close(); } catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("cookie write to file");
}
}
}
#CookieLogin.java

package com.meicai.tms;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; public class CookieLogin { /**
* @author Young
* @param args
*/ public static void main(String[] args) {
// TODO Auto-generated method stub
//Cookies.addCookies();
WebDriver driver = new ChromeDriver();
driver.get("http://weibo.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try
{
File file=new File("D:\\broswer.data");
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!= null)
{
StringTokenizer str=new StringTokenizer(line,";");
while(str.hasMoreTokens())
{
String name=str.nextToken();
String value=str.nextToken();
String domain=str.nextToken();
String path=str.nextToken();
Date expiry=null;
String dt;
if(!(dt=str.nextToken()).equals(null))
{
//expiry=new Date(dt);
System.out.println();
}
boolean isSecure=new Boolean(str.nextToken()).booleanValue();
Cookie ck=new Cookie(name,value,domain,path,expiry,isSecure);
driver.manage().addCookie(ck);
}
} }
catch(Exception e)
{
e.printStackTrace();
} driver.get("http://weibo.com/");
System.out.println("登陆成功!");
} }

java 获取cookie的更多相关文章

  1. Java通过httpclient获取cookie模拟登录

    package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...

  2. js/java 获取、添加、修改、删除cookie(最全)

      一.cookie介绍 1.cookie的本来面目 HTTP协议本身是无状态的.什么是无状态呢,即服务器无法判断用户身份.Cookie实际上是一小段的文本信息(key-value格式).客户端向服务 ...

  3. 利用Python获取cookie的方法,相比java代码简便不少

    1.通过urllib库,是python的标准库,不需要另外引入,直接看代码,注意代码的缩进: # coding=UTF-8import cookielibimport urllib2 class Ry ...

  4. 通过js获取cookie的实例及简单分析

    今天碰到一个在firefox下swfupload 上传时session不一致问题 在一个项目遇到多文件上传时,firefox下,服务器端的session获取不一致问题. 解决办法: 解决办法:将ses ...

  5. java对cookie的操作

    java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的生命周期和cookie的路径问题. 建立一个无生命周期的cookie,即随着浏览器的关闭即 ...

  6. java之Cookie详解

    Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是 ...

  7. JAVA操作COOKIE

    JAVA操作COOKIE 1.设置Cookie Cookie cookie = new Cookie("key", "value"); cookie.setMa ...

  8. 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录

    利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...

  9. JSP获取Cookie对象

    cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器的.通过使用cookie可以标识用户身份,记录用户和密码,跟踪重复用户等.浏览器将cookie以key/value的形式保存到客户机的某 ...

随机推荐

  1. TCP 三次握手 四次握手

    http://blog.chinaunix.net/uid-22312037-id-3575121.html http://www.centos.bz/2012/08/tcp-establish-cl ...

  2. SSH框架搭建和整合(struts2、spring4、hibernate5)

    声明: 本博文是个人通过对ssh框架的学习.理解还有一些看法而描述出来的,可能有不足之处,请大家谅解,但希望能帮助到大家! 目的: 使初学者能更好的去了解SSH框架. 给以后的自己,也给别人一个参考. ...

  3. [转] geochart 地图控件官方示例

    <html> <head> <script type='text/javascript' src='https://www.google.com/jsapi'>&l ...

  4. JasperReport报表导出踩坑实录

    写在最前面 翻了翻博客,因为太忙,已经好久没认真总结过了. 正好趁着今天老婆出门团建的机会,记录下最近这段时间遇到的大坑-JasperReport. 六月份的时候写过一篇利用poi文件导入导出的小De ...

  5. Proof for Floyd-Warshall's Shortest Path Derivation Algorithm Also Demonstrates the Hierarchical Path Construction Process

    (THIS BLOG WAS ORIGINALLY WRTITTEN IN CHINESE WITH LINK: http://www.cnblogs.com/waytofall/p/3732920. ...

  6. GeoServer之图层的新建与发布

    GeoServer之图层的新建与发布 GeoServer的图层发布并不复杂,在经过: 1.创建工作区 2.添加新的数据存储 3.编写styles 后:我们就可以很简单的创建图层了. 1.在新建图层中选 ...

  7. Halcon学习之七:改变图像的现实方式和大小

    change_format ( Image : ImagePart : Width, Height : ) 改变Image图像大小,而且ImagePart图像为灰度值图像. crop_domain ( ...

  8. fileinput模块可以循环一个或多个文本文件的内容

    fileinput模块可以循环一个或多个文本文件的内容. [默认格式] fileinput.input (files=None, inplace=False, backup='', bufsize=0 ...

  9. PHP函数内访问全局变量

    $dbcon='123'; 方法一.funtion fun1(){global $dbcon;$dbcon-> 就可以访问了.} 方法二$GLOBALS['$dbcon'];

  10. shell编程——内部变量

    常用的内部变量有:echo, eval, exec, export, readonly, read, shift, wait, exit 和 点(.) echo:将变量名指定的变量显示到标准输出 [r ...