-
Notifications
You must be signed in to change notification settings - Fork 0
Get login info from cookie
Yuqi Wu edited this page Feb 10, 2017
·
1 revision
介绍一种通过Cookie记录login信息的方法,可以绕过登陆操作及登陆验证码操作。Code as follows:
private static void getLogin(String url,String cookie,WebDriver driver){
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(url); // 需手动访问一次测试页面获得cookie
driver.manage().deleteAllCookies(); // 删除cookie里的内容
Cookie ck = new Cookie("PHPSESSID", cookie); // 初始化已经保存了登录信息的cookie
driver.manage().addCookie(ck); // webdriver添加cookie
driver.get(url); // 访问后就会发现已经登录成功了
//driver.manage().window().maximize(); //将浏览器最大化
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);;
Set<Cookie> cookies = driver.manage().getCookies();
System.out.println(String.format("Domain -> name -> value -> expiry -> path"));
for (Cookie c : cookies)
System.out.println(String.format("%s -> %s -> %s -> %s -> %s", c.getDomain(), c.getName(), c.getValue(),c.getExpiry(), c.getPath()));
}