2014-05-09 6 views
2

Gmail 로그인을 위해 모든 쿠키를 저장합니다. Gmail에 다시 로그인 할 때이 쿠키를 사용합니다.이 쿠키 파일을로드하지만이 쿠키로 로그인 할 수 없습니다. 과 같은 메시지가 표시됩니다. ""메인 "스레드의 예외"org.openqa.selenium.InvalidCookieDomainException : 긴 검색 나는이에 대한 솔루션이나 해결 방법을 얻을 수 없다받은 후selenium webdriver를 사용한 다중 도메인 쿠키 처리

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

     } 
    } 

: 현재 도메인 "

내 코드는 다음과 같다이다.

단일 로그인으로 다중 도메인의 유효성을 검사 할 때 이러한 유형의 오류가 발생했음을 이해합니다.

정말 쿠키를 사용하여 Gmail에 로그인해야합니다. 이것은 예를 들어, 이것이 구현 된 여러 사이트가 있는데, 우리는 이것을 셀레늄 webdriver에서 어떻게 처리 할 수 ​​있습니까?

답변

1

문제가되는 도메인이나 쿠키가 명확하지 않습니다. 이 간단한 코드는 나를 위해 작동합니다.

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에서 쿠키에 와일드 카드 도메인을 반환합니다. 따라서 쿠키를 설정하기 전에 유효한 하위 도메인을 열어야합니다. Google Mail은 .mail.google.complus.google.com의 쿠키도 설정합니다. 따라서 모든 쿠키를 검사하고 설정하기 전에 각 쿠키의 유효한 도메인을여십시오.

Google에서 사용자를 리디렉션 할 수 있기 때문에 문제가 발생할 수 있습니다. 예를 들어 https://mail.google.com/을 열어 로그인하지 않은 경우 https://accounts.google.com으로 리디렉션됩니다.

+0

당신의 도움에 감사드립니다 .... 내가 주어진 질문을 수정한다. 내가 실제로하고있는 것을 이해할 수있는 코드를 더 추가한다. 주요 문제 제작자는 .main.google.com이 코드에서 난장판을 만든다면 코드를 수정하십시오. – saba

+0

내 코드를 살펴 본다면 Gmail에 로그인하여 모든 쿠키를 파일에 쓰고 브라우저를 닫은 다음 다시 브라우저를 실행하고 파일에서 쿠키를 제공하는 방법을 확인하십시오. 또한 페이스 북을 사용하여 로그인합니다. 페이 스북 (Facebook)에서 쿠키를 사용하면 왜 Gmail이 아닌지 멀티 도메인 문제를 말할 수 있지만 어떻게 해결할 수 있습니까? – saba

+1

코드를 사용해 보았지만 제대로 작동하지 않았습니다. '''mail.google.com''을 호출하면''''accounts.google.com''으로 리디렉션되므로 셀레늄과 함께 쿠키를 설정할 수 없습니다. 하지만'''.google.com''의 모든 쿠키를 사용하면 로그인하고 예를 들어 https://www.google.com/settings/personalinfo. 다른 응용 프로그램 (https://developers.google.com/accounts/docs/OAuth2Login)에 도움이 될만한 oAuth-Token이 필요합니다. – lefloh

관련 문제