Java 从一个硒会话到另一个会话长期保存cookie的最佳方法?
我正在使用Java 从一个硒会话到另一个会话长期保存cookie的最佳方法?,java,selenium,selenium-webdriver,firefox,cookies,Java,Selenium,Selenium Webdriver,Firefox,Cookies,我正在使用seleniumwebdriver启动一个特定的Firefox配置文件。通常,Selenium会创建一个模拟现有Firefox配置文件的FF临时安装,然后在调用webdriver.close()后删除该配置文件 但是,我需要能够通过Selenium启动Firefox WebDriver,并为将来的会话保留某些登录cookie,这样我就不会在每次登录帐户时都被要求输入密码和/或验证码。我见过几种解决这个问题的方法,但每种方法都有自己的问题: 1)识别Selenium创建的临时文件,并将c
seleniumwebdriver
启动一个特定的Firefox配置文件。通常,Selenium会创建一个模拟现有Firefox配置文件的FF临时安装,然后在调用webdriver.close()
后删除该配置文件
但是,我需要能够通过Selenium
启动Firefox WebDriver
,并为将来的会话保留某些登录cookie,这样我就不会在每次登录帐户时都被要求输入密码和/或验证码。我见过几种解决这个问题的方法,但每种方法都有自己的问题:
1)识别Selenium创建的临时文件,并将cookies.sqlite
复制到原始FF配置文件安装文件夹中。我看到这种方法的问题是,我同时启动了许多Selenium实例,因此如何确定哪个临时文件夹对应于特定的WebDriver会话?另外,是否可以将cookie附加到原始安装文件,而不是覆盖原始文件
2)一个呼叫:
我发现这种方法的问题是,我需要长期(即永远)保存cookie,并且我无法在内存中存储所有会话的cookie。即使可以,当程序存在或主机关闭时,cookie数据也会丢失。此外,getCookies()
只返回当前域的cookies,而addCookie()只允许添加域与当前URL的域相同的cookie。因此,在获取或设置cookies时,如前所述,首先转到正确的URL是很重要的。我想在启动新的FF会话时立即从多个域加载Cookie
3)另一个选项可能是首先防止Firefox在新位置创建临时文件(即重定向到原始FF安装目录),并防止FF在退出时删除临时文件。这样做可行吗
我见过其他一些方法,但它们要么只解决这个问题,因为它与Chrome(而不是Firefox)或Python有关,要么它们存在上述缺陷之一
在Java+Firefox中可以使用什么方法来解决这个问题
谢谢 对于您来说,最好的解决方案是将它们存储在JSON文件中,这样您以后就可以将它们加载到浏览器中 以下是如何存储它们的快速教程: 使用JSON编写:
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class WriteJSONExample
{
@SuppressWarnings("unchecked")
public static void main( String[] args )
{
//First Employee
JSONObject employeeDetails = new JSONObject();
employeeDetails.put("firstName", "Lokesh");
employeeDetails.put("lastName", "Gupta");
employeeDetails.put("website", "howtodoinjava.com");
JSONObject employeeObject = new JSONObject();
employeeObject.put("employee", employeeDetails);
//Second Employee
JSONObject employeeDetails2 = new JSONObject();
employeeDetails2.put("firstName", "Brian");
employeeDetails2.put("lastName", "Schultz");
employeeDetails2.put("website", "example.com");
JSONObject employeeObject2 = new JSONObject();
employeeObject2.put("employee", employeeDetails2);
//Add employees to list
JSONArray employeeList = new JSONArray();
employeeList.add(employeeObject);
employeeList.add(employeeObject2);
//Write JSON file
try (FileWriter file = new FileWriter("employees.json")) {
//We can write any JSONArray or JSONObject instance to the file
file.write(employeeList.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
阅读JSON:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadJSONExample
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("employees.json"))
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray employeeList = (JSONArray) obj;
System.out.println(employeeList);
//Iterate over employee array
employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private static void parseEmployeeObject(JSONObject employee)
{
//Get employee object within list
JSONObject employeeObject = (JSONObject) employee.get("employee");
//Get employee first name
String firstName = (String) employeeObject.get("firstName");
System.out.println(firstName);
//Get employee last name
String lastName = (String) employeeObject.get("lastName");
System.out.println(lastName);
//Get employee website name
String website = (String) employeeObject.get("website");
System.out.println(website);
}
}
这个答案很模糊。。我不认为它解决了我在selenium自动装置中使用的问题。您只需将cookie保存在JSON文件中,就可以将它们加载到selenium的另一个会话中。这样,您的cookies将在整个会话中保存,您可以随时使用它们。感谢您的澄清。您是否介意提供一个完整的例子,说明如何在Java中实现这一点以及您的答案?然后我会接受你的答案。我用一个清晰的例子编辑了我的答案。首先,我如何获得所有cookie(来自所有域),以便将它们转换为JSON?正如我在OP中所说,WebDriver只从当前URL返回cookies。另外,当我再次启动WebDriver时,如何将它们加载到WebDriver中?谢谢
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class WriteJSONExample
{
@SuppressWarnings("unchecked")
public static void main( String[] args )
{
//First Employee
JSONObject employeeDetails = new JSONObject();
employeeDetails.put("firstName", "Lokesh");
employeeDetails.put("lastName", "Gupta");
employeeDetails.put("website", "howtodoinjava.com");
JSONObject employeeObject = new JSONObject();
employeeObject.put("employee", employeeDetails);
//Second Employee
JSONObject employeeDetails2 = new JSONObject();
employeeDetails2.put("firstName", "Brian");
employeeDetails2.put("lastName", "Schultz");
employeeDetails2.put("website", "example.com");
JSONObject employeeObject2 = new JSONObject();
employeeObject2.put("employee", employeeDetails2);
//Add employees to list
JSONArray employeeList = new JSONArray();
employeeList.add(employeeObject);
employeeList.add(employeeObject2);
//Write JSON file
try (FileWriter file = new FileWriter("employees.json")) {
//We can write any JSONArray or JSONObject instance to the file
file.write(employeeList.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadJSONExample
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("employees.json"))
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray employeeList = (JSONArray) obj;
System.out.println(employeeList);
//Iterate over employee array
employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private static void parseEmployeeObject(JSONObject employee)
{
//Get employee object within list
JSONObject employeeObject = (JSONObject) employee.get("employee");
//Get employee first name
String firstName = (String) employeeObject.get("firstName");
System.out.println(firstName);
//Get employee last name
String lastName = (String) employeeObject.get("lastName");
System.out.println(lastName);
//Get employee website name
String website = (String) employeeObject.get("website");
System.out.println(website);
}
}