2012-07-12 141 views
1

我使用Selenium Webdriver,我想在执行测试之前清除cookie。我使用官方的'Selenium'网站的代码。这是代码:如何正确删除Cookie?

Cookie cookie = new Cookie("key", "value"); 
driver.manage().addCookie(cookie); 
Set<Cookie> allCookies = driver.manage().getCookies(); 
for (Cookie loadedCookie : allCookies) { 
    System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue())); 
} 
driver.manage().deleteAllCookies(); 

,但我得到通知:- Cookie cannot be resolved to a type, Set cannot be resolved to a type

回答

2

Setjava.util包。

Cookieorg.openqa.selenium包。

您需要导入这两个类让你的代码工作:

import java.util.Set; 
import org.openqa.selenium.Cookie; 

为了让痛苦减少痛苦,每一个现代的Java IDE具有这种自动功能:

  • 在Eclipse ,它被称为“组织进口”,并且坐在对照下 + 移位 + O
  • 在的IntelliJ,这就是所谓的 “优化进口” 和下按Ctrl + Alt键+Ø坐在
  • 在NetBeans中,它也被称为不知何故,坐在下按Ctrl ++
0

我会检查你的进口。我怀疑当你需要硒时,你正在使用javax cookie。

javax.servlet.http.Cookie 

org.openqa.selenium.Cookie 
+0

就是这样,第一个通知消失了,比我添加org.openqa.selenium.Cookie导入,但第二个(Set不能解析为一个类型)仍然是。比我添加javax.servlet.http.Cookie - 再次出现两个通知 – khris 2012-07-12 10:52:06