【问题标题】:How to export/import cookies using Java + Selenium WebDriver如何使用 Java + Selenium WebDriver 导出/导入 cookie
【发布时间】:2019-02-09 01:02:51
【问题描述】:

我有一个使用 Java + Selenium WebDriver 的工具,我每天都在运行它。如何导出 cookie、历史记录...并像普通浏览器一样导入/重用它以供下次执行。

【问题讨论】:

  • 在加载浏览器时加载配置文件。

标签: java selenium selenium-webdriver selenium-firefoxdriver


【解决方案1】:

我们可以将浏览器的配置文件信息写入 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2012-05-07
    • 2023-01-16
    • 1970-01-01
    相关资源
    最近更新 更多