Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用selenium webdriver处理多域Cookie_Java_Cookies_Selenium_Selenium Webdriver - Fatal编程技术网

Java 使用selenium webdriver处理多域Cookie

Java 使用selenium webdriver处理多域Cookie,java,cookies,selenium,selenium-webdriver,Java,Cookies,Selenium,Selenium Webdriver,我保存所有用于gmail登录的cookies。我在gmail中再次登录时使用了此cookie我加载了此cookies文件,但我无法使用此cookies登录。在线程“main”org.openqa.selenium.InvalidCookieDomainException中获取异常,如“异常:您只能为当前域设置cookies” 我的代码如下所示: File f = new File("c:\\browser.data"); WebDriver driver = new Firefo

我保存所有用于gmail登录的cookies。我在gmail中再次登录时使用了此cookie我加载了此cookies文件,但我无法使用此cookies登录。在线程“main”org.openqa.selenium.InvalidCookieDomainException中获取异常,如“异常:您只能为当前域设置cookies”

我的代码如下所示:

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

        }
    }
经过长时间的搜索,我无法找到任何解决方案或解决办法

据我所知,这种类型的错误发生在单次登录验证多个域时


我真的需要使用cookies登录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之前,必须打开一个有效的子域。google mail还在为
.mail.google.com
plus.google.com
设置cookies。因此,在设置cookie之前,请检查所有cookie并为每个cookie打开一个有效域


这可能会变得棘手,因为谷歌可能会重定向你。例如,如果我正在打开而未登录,我将重定向到。

感谢您的帮助……我修改了给定的问题,添加了更多代码,您可以通过这些代码了解我的实际操作。在这里,您可以了解主要问题的创建者是。main.google.com请检查我的代码,如果我的代码混乱,请纠正我。如果你查看我的代码,然后看到我登录我的gmail添加将所有cookie写入文件并关闭浏览器,然后再次启动浏览器并从文件中提供cookies……我也使用facebook执行此操作,我使用cookies登录facebook,为什么不使用gmail,你可以说是多域问题,但我如何解决这个问题请帮助我。尝试了你的代码,但没有得到它的工作。调用
mail.google.com
会将我重定向到
accounts.google.com
,因此无法使用selenium设置cookie。但是,使用来自“`.google.com``的所有cookie,您已登录并可以访问,例如。我想你需要一个oAuth令牌来继续其他应用程序,所以也许吧。谢谢你的宝贵意见…你能给一些代码片段如何在selenium webdriver中使用它吗。如果你能,它真的能帮助我。。。。。