java - 如何使用 Java + Selenium WebDriver 导出/导入 cookie
问题描述
我有一个使用 Java + Selenium WebDriver 的工具,我每天都运行它。如何导出 cookie、历史记录......并像普通浏览器一样导入/重用它以供下次执行。
解决方案
我们可以将浏览器的配置文件信息写入 JSON 文件,然后用相同的配置文件实例化新的浏览器。
FirefoxProfile类提供toJson()方法写入配置文件信息
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);
推荐阅读
- architecture - NetWeight 和 GrossWeight 作为单独的值对象
- vue.js - Vuex 和 Vue-router:同时改变页面和状态
- c - C 中的内存泄漏 (MacOS)
- git - 如何按名称查找正在或曾经在远程分支中的文件?
- java - 用于postgres时间戳字段的Spring Jpa LessThanEqual
- java - Spring:无法使用选定字段查询存储库
- flutter - 固定错误:如何解决返回类型'StreamController
' 不是匿名闭包错误所定义的 'Stream' - java - javac无法在项目中导入类
- python - 如何将多行 JSON 字符串作为参数传递给 Python 脚本?
- flutter - 如果不支持颤振库,则忽略