public static void main(String[] args) { String url = "http://www.cnblogs.com/LoveTest/"; WebDriver dr = new FirefoxDriver(); dr.navigate().to(url); //初始化一个cookie对象 Cookie cookie = new Cookie("cookieName","cookieValue"); //将该cookie添加到当前窗口 dr.manage().addCookie(cookie); //获取当前窗口的所有Cookie Set<Cookie> cookies = dr.manage().getCookies(); System.out.print(String.format("domain->name->value->expiry->path\n")); for(Cookie c:cookies) { System.out.print(c.getDomain()+"->"+c.getName()+"->"+c.getValue()+"->"+c.getExpiry()+"->"+c.getPath()+"\n"); } /* * 输出结果: * domain->name->value->expiry->path * .cnblogs.com->_gat->1->Thu Nov 20 17:36:20 CST 2014->/ * .cnblogs.com->_ga->GA1.2.893092694.1416475580->Sat Nov 19 17:26:20 CST 2016->/ * www.cnblogs.com->cookieName->cookieValue->null->/ */ //删除cookie对象 dr.manage().deleteCookie(cookie); //根据cookie名称删除cookie //dr.manage().deleteCookieNamed("cookieName"); //删除所有cookie //dr.manage().deleteAllCookies(); }
上面的代码首先在页面中增加了一个cookie,然后遍历页面的所有cookies,并输出他们的主要属性。最后就是
三种删除cookie的方法。
时间: 2024-12-25 23:48:44