【问题标题】:Issue in cookies in selenium硒中的cookie问题
【发布时间】:2017-02-26 10:20:31
【问题描述】:

我想要做的是将cookies保存在一个文件中并重复使用该cookies以避免每次登录,但我面临的错误是

[org.openqa.selenium.InvalidCookieDomainException:您只能为当前域设置cookie]

下面我附上我的代码。 [阅读 Cookie 的代码]

public static WebDriver driver; 

public static void main(String[] args) throws InterruptedException                  
{       

    driver=new FirefoxDriver();     
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");


    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);                 

    driver.findElement(By.xpath("//input[@name='Email']")).sendKeys("<<USERNAME>");                         
    driver.findElement(By.xpath("//input[@id='next']")).click();    
    driver.findElement(By.xpath("//input[@name='Passwd']")).sendKeys("<<PASSWORD>>");   
    driver.findElement(By.id("signIn")).click();                    


    File file = new File("Cookies.data");                           
    try     
    {       
        // Delete old file if exists
        file.delete();      
        file.createNewFile();           
        FileWriter fileWrite = new FileWriter(file);                            
        BufferedWriter Bwrite = new BufferedWriter(fileWrite);                          
        // loop for getting the cookie information      
        for(Cookie ck : driver.manage().getCookies())



        {       
            Bwrite.write((ck.getName()+";"+ck.getValue()+";"+ck.getDomain()+";"+ck.getPath()+";"+ck.getExpiry()+";"+ck.isSecure()));                                                                                                    
            Bwrite.newLine();   
            System.out.println(ck.getExpiry());
        }       
        Bwrite.flush();         
        Bwrite.close();         
        fileWrite.close();          
    }catch(Exception ex)                    
    {       
        ex.printStackTrace();           
    }       
}           
}

[使用该 Cookie 的代码]

public  static WebDriver driver;                
public static void main(String[] args)                  
{                           
    driver=new FirefoxDriver(); 

    try{            

        File file = new File("Cookies.data");                           
        FileReader fileReader = new FileReader(file);                           
        BufferedReader Buffreader = new BufferedReader(fileReader);                         
        String strline;         
        while((strline=Buffreader.readLine())!=null){                                   
            StringTokenizer token = new StringTokenizer(strline,";");                                   
            while(token.hasMoreTokens()){                   
                String name = token.nextToken();                    
                String value = token.nextToken();                   
                String domain = token.nextToken();                  
                String path = token.nextToken();                    
                Date expiry =  null;                    

                String val;         
                if(!(val=token.nextToken()).equals("null"))
                {       
                    SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
                    expiry =  df.parse(val); 
                }       
                Boolean isSecure = new Boolean(token.nextToken()).                              
                        booleanValue();     

                Cookie ck = new Cookie(name,value,domain,path,expiry,isSecure);     

                driver.manage().addCookie(ck);
                System.out.println(ck.getDomain());
                System.out.println(ck);
            }       
        }       
    }catch(Exception ex){                   
        ex.printStackTrace();           
    }       
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");  
}

【问题讨论】:

标签: java selenium


【解决方案1】:

在读取的 cookie java 类中,在驱动程序实例化之后立即包含一行(复制粘贴),以调用页面,就像您在最后所做的那样 - driver.get(https://accounts..........

这允许将 cookie 设置为好像它们来自同一个域。

【讨论】:

  • 我已经尝试过您的方法,但仍然遇到同样的错误。
  • 更清楚一点:您只能获取/设置当前浏览上下文的活动文档的 cookie。这意味着您必须在尝试获取/设置关联的 cookie 之前从域导航到页面。
猜你喜欢
  • 2017-12-08
  • 2018-10-22
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 1970-01-01
相关资源
最近更新 更多