我保存所有用於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