简体   繁体   English

如何使用Java + Selenium WebDriver导出/导入Cookie

[英]How to export/import cookies using Java + Selenium WebDriver

I have a tool using Java + Selenium WebDriver and I run it every day. 我有一个使用Java + Selenium WebDriver的工具,并且每天都在运行。 How can I export the cookies, histories... and import/reuse it for next execution like a normal browser. 我如何导出Cookie,历史记录...以及将其导入/重用,以便像普通浏览器一样用于下次执行。

We can write the profile information of the browser to the JSON file and later instantiate new browsers with the same profile. 我们可以将浏览器的配置文件信息写入JSON文件,然后使用相同的配置文件实例化新的浏览器。

FirefoxProfile class provides toJson() method to write profile information FirefoxProfile类提供toJson()方法来写入配置文件信息

FirefoxProfile class provides fromJson() method to retrieve profile information FirefoxProfile类提供fromJson()方法来检索配置文件信息

FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(
new File("src/test/resources/extensions/anyextenstion.file"));
String json = profile.toJson();
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(FirefoxProfile.fromJson(json));
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM