在selenium中设置cookie

时间:2016-06-29 12:59:16

标签: java selenium cookies selenium-webdriver

我正在尝试使用以下代码设置Cookie

WebDriver driver=new FirefoxDriver();
driver.get("https://mail.google.com");
driver.findElement(By.id("Email")).sendKeys("example@gmail.com");
driver.findElement(By.id("next")).click();
Thread.sleep(3000);
driver.findElement(By.id("Passwd")).sendKeys("example999");
driver.findElement(By.id("signIn")).click();
Thread.sleep(3000);
Set<Cookie> cookies=driver.manage().getCookies();
driver.quit();
driver=new FirefoxDriver();
for(Cookie a:cookies){  
    driver.manage().addCookie(a);   
}
driver.get("https://mail.google.com");

它抛出的错误如下:

org.openqa.selenium.InvalidCookieDomainException:
You may only set cookies for the current domain
Command duration or timeout: 55 milliseconds

1 个答案:

答案 0 :(得分:0)

我有一些我在c#中完成的事情看看

for (int i = 0; i < driver.Manage().Cookies.AllCookies.Count - 1; i++)
{
   System.Net.Cookie ck = new
   System.Net.Cookie(driver.Manage().Cookies.AllCookies[i].Name, driver.Manage().Cookies.AllCookies[i].Value, driver.Manage().Cookies.AllCookies[i].Path, driver.Manage().Cookies.AllCookies[i].Domain);
   //here you can add it to your cookie list.
}