【问题标题】:java.lang.IllegalArgumentException when trying to read data from cookies in selenium webdriver尝试从 selenium webdriver 中的 cookie 读取数据时出现 java.lang.IllegalArgumentException
【发布时间】:2015-09-22 04:49:58
【问题描述】:

我编写了几行代码,从存储在文本文件中的 cookie 中读取数据,然后在需要时将其写入网络浏览器。

我用来在文本文件中存储和写入 cookie 的代码块-

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class StoreCookieInfo {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Java Programs and files\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
driver.findElement(By.name("email")).sendKeys("Your username");
driver.findElement(By.name("pass")).sendKeys("Your password");
driver.findElement(By.name("persistent")).click();
driver.findElement(By.name("pass")).submit();

File f = new File("browser.data");
try{
     f.delete();
     f.createNewFile();
     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();
     bos.close();
     fos.close();
 }catch(Exception ex){
     ex.printStackTrace();
 }

  }
 }

它运行良好并将数据存储在文本文件中。 我用来从文本文件中读取 cookie 的代码块-

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.StringTokenizer;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LoadCookieInfo {

@SuppressWarnings("deprecation")
public static void main(String[] args){
    System.setProperty("webdriver.chrome.driver","D:\\Java Programs and files\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    //WebDriver driver = new FirefoxDriver();
    driver.get("http://www.facebook.com");
    try{
         File f2 = new File("browser.data");
         FileReader fr = new FileReader(f2);
         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();
                 System.out.println("1");
                 Date expiry = null;
                 String dt;
                 if(!(dt=str.nextToken()).equals("null")){
                     expiry = new Date(dt);
                 }
                 boolean isSecure = new Boolean(str.nextToken()).booleanValue();
                 Cookie ck = new Cookie(name,value,domain,path,expiry,isSecure);
                 driver.manage().addCookie(ck);
                 System.out.println(name+value);
             }
         }
    }catch(Exception ex){
         ex.printStackTrace();
    }
    driver.get("http://www.facebook.com");
 }
}

当我尝试运行第二个代码时,我在日期收到以下异常 -

java.lang.IllegalArgumentException
at java.util.Date.parse(Unknown Source)
at java.util.Date.<init>(Unknown Source)
at com.Selenium_Practice.LoadCookieInfo.main(LoadCookieInfo.java:39)

这是我第一次尝试使用 cookie 读取数据,所以我无法弄清楚我在代码中做错了什么。

【问题讨论】:

    标签: java selenium cookies selenium-webdriver


    【解决方案1】:

    抛出异常java.lang.IllegalArgumentException 是因为您将String 作为参数传递给Date constructor,该Date constructor 已被弃用并替换为DateFormat.parse(String s)

    String dt;
      if(!(dt=str.nextToken()).equals("null")){
        expiry = new Date(dt);
      }
    

    尝试使用:

    SimpleDateFormat formatter = new SimpleDateFormat("("E MMM dd HH:mm:ss Z yyyy")");
    Date date = formatter.parse(dt);
    

    您的 if 条件似乎也有问题。 (dt=str.nextToken()) 将分配值并始终为您提供true,永远不会等于null

    让我知道它是否适用于上述解决方案。

    【讨论】:

    • 修改代码后出现同样的异常
    【解决方案2】:

    在 java 中使用 SimpleDateFormat 类将字符串转换为日期对象。它是用于格式化和解析日期的具体类。它允许您从选择任何用户定义的日期时间格式模式开始

    browser.data 文件中,日期以Sat Oct 03 01:12:17 IST 2015 格式保存

    所以使用SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy")

    E-->星期名(Sat)

    MMM----->月份 (Oct)

    dd------>月份中的某天(03)

    HH:mm:ss---->时:分:秒(01:12:17)

    Z-->时区(IST)

    yyyy---->年份(2015)

    Date expiry = null;
    SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
    
                    try {
                         String dt;
                         if(!(dt=str.nextToken()).equals("null")){
                        expiry = formatter.parse(dt);
                        System.out.println(expiry);
                        System.out.println(formatter.format(expiry));
                         }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
    

    我测试了上面的代码,它工作正常。在将上述更改应用于 LoadCookieInfo 类后,我能够成功添加 cookie

    希望这对您有所帮助...如果您有任何疑问,请回复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2022-06-14
      • 2014-01-28
      相关资源
      最近更新 更多