我保存所有用于 gmail 登录的 cookie。我在 gmail 中再次登录时使用此 cookie 我加载了此 cookie 文件,但我无法使用此 cookie 登录 出现异常,如 "线程 "main"中的异常 org.openqa.selenium.InvalidCookieDomainException: 你可能只为当前域设置 cookie"
我的代码是这样的:
File f = new File("c:\\browser.data");
WebDriver driver = new FirefoxDriver(fb, fp);
driver.get("https://accounts.google.com");
driver.findElement(By.name("Email")).sendKeys("myusername");
driver.findElement(By.name("Passwd")).sendKeys("mypassword");
driver.findElement(By.name("PersistentCookie")).click();
driver.findElement(By.name("signIn")).submit();
Thread.sleep(20000);
try {
f.delete();
f.createNewFile();
try (FileWriter fos = new FileWriter(f); BufferedWriter bos = new BufferedWriter(fos)) {
for (Cookie ck : driver.manage().getCookies()) {
bos.write((ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure()));
bos.newLine();
}
bos.flush();
}
} catch (IOException ex) {
ex.printStackTrace();
}
driver.findElement(By.cssSelector(".gb_V.gbii")).click();
driver.findElement(By.xpath(".//*[@id='gb_71']")).click();
driver.close();
WebDriver driver1 = new FirefoxDriver(pf);
driver1.get("https://accounts.google.com");
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 SimpleDateFormat("EEE MMM d H:m:s z y").parse(dt);
}
boolean isSecure = Boolean.valueOf(str.nextToken()).booleanValue();
Cookie ck1 = new Cookie(name, value, domain, path, expiry, isSecure);
System.out.println(domain);
if (domain.equalsIgnoreCase(".google.com")) {
driver1.get("https://accounts.google.com/ ");
driver1.manage().addCookie(ck);
driver1.get("https://accounts.google.com/ ");
}
if (domain.equalsIgnoreCase(".mail.google.com")) {
//driver1.get("http://accounts.google.com");
driver1.get("http://mail.google.com");
Thread.sleep(10000);
driver1.manage().addCookie(ck);
//driver1.get("http://accounts.google.com");
driver1.get("http://mail.google.com");
}
}
}
经过长时间的搜索,我找不到任何解决方案或解决方法。
据我了解,此类错误发生在单次登录验证多个域时。
我真的需要使用 cookie 登录 gmail。这是一个例子,有几个站点实现了这个,那么我们如何在 selenium webdriver 中处理这个?
最佳答案
目前尚不清楚是哪个域或哪个 cookie 导致了问题。这个简单的代码对我有用:
driver.get("https://accounts.google.com");
Cookie cookie = new Cookie("foo", "bar", ".google.com", "/", new Date(), true);
driver.manage().addCookie(cookie);
driver.get("https://accounts.google.com“);
请注意,google 在 cookie 中返回一个通配符域。因此,您必须在设置 cookie 之前打开一个有效的子域。 GoogleMail 还为 .mail.google.com
和 plus.google.com
设置了 cookie。所以在设置之前检查所有cookies并为每个cookie打开一个有效域。
这可能会变得棘手,因为谷歌可能会重定向您。例如,如果我打开 https://mail.google.com/我没有登录我正在重定向到 https://accounts.google.com .
关于java - 使用 selenium webdriver 处理多域 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23567123/