我保存所有用于gmail登录的cookie。我使用这个cookie当我再次登录gmail我加载这个cookie文件,但我不能用这个cookie登录得到异常像“异常在线程”主“org.openqa.selenium.InvalidCookieDomainException:您可能只设置cookie当前域“使用硒webdriver处理多个域名的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。这是一个例子,有几个网站,这实现了那么我们如何处理这在硒webdriver?
感谢您的帮助....我修改了我提供的问题,我添加了更多的代码,通过它可以了解我在做什么。您可以了解主要问题创建者是.main.google.com,请通过我的如果我在代码中弄乱了代码并纠正我。 – saba
如果你通过我的代码然后看到我登录我的gmail添加写入所有cookie到一个文件并关闭浏览器,并再次启动浏览器并从文件中提供cookie ......我也这样做与Facebook和我登录在脸书使用cookies,为什么不为Gmail,你可以说多域问题,但我该如何解决这个问题,请帮助我。 – saba
试过你的代码,并没有得到它的工作。调用mail.google.com'''会将我重定向到'''accounts.google.com''',因此无法使用selenium设置cookie。但是,来自''''.google.com'''的所有Cookie都已登录,并且可以访问https://www.google.com/settings/personalinfo。我想你需要一个oAuth-Token才能进入其他应用程序,所以可能[这有助于](https://developers.google.com/accounts/docs/OAuth2Login)。 – lefloh