简体   繁体   中英

Multiple domain cookies handling using selenium webdriver

I save all cookies for gmail login. I use this cookies when I login again in gmail I load this cookies file but I can not login with this cookies get exception like " Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain "

My code is look like:

    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");
                }

        }
    }

After getting long search I cannot get any solution or workaround for this.

As I understand that this type of error occurred when single login validate the multiple domain.

I really need to login in gmail using cookies. This is for an example there are several site where this implements then how can we handle this in selenium webdriver?

It's not clear which domain or which cookie is causing trouble. This simple code is working for me:

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“);

Note that google is returning a wildcard-domain in the cookie. So you must open a valid subdomain before you set the cookie. GoogleMail is also setting cookies for .mail.google.com and plus.google.com . So check all cookies and open a valid domain for each cookie before setting it.

This can get tricky because google might redirect you. For instance if I'm opening https://mail.google.com/ and I'm not logged in I'm getting a redirect to https://accounts.google.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM