[英]Multiple domain cookies handling using selenium webdriver
我保存了所有Cookie以進行gmail登錄。 我再次使用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。 因此,在設置之前,請檢查所有cookie並為每個cookie打開有效域。
這可能會變得棘手,因為Google可能會重定向您。 例如,如果我打開https://mail.google.com/而沒有登錄,那么我將重定向到https://accounts.google.com 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.