java.lang.IllegalArgumentException 尝试从 selenium webdriver 中的 cookie 中读取数据时

java.lang.IllegalArgumentException when trying to read data from cookies in selenium webdriver

我写了几行代码,从存储在文本文件中的 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.lang.IllegalArgumentException 是因为您将字符串作为参数传递给 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

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

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

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

我测试了上面的代码它正在工作 fine.I 在将上述更改应用到 LoadCookieInfo 之后能够成功添加 cookie class

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