user2558093
user2558093

Reputation: 33

How to export/import cookies using Java + Selenium WebDriver

I have a tool using Java + Selenium WebDriver and I run it every day. How can I export the cookies, histories... and import/reuse it for next execution like a normal browser.

Upvotes: 0

Views: 768

Answers (1)

Amit Jain
Amit Jain

Reputation: 4587

We can write the profile information of the browser to the JSON file and later instantiate new browsers with the same profile.

FirefoxProfile class provides toJson() method to write profile information

FirefoxProfile class provides fromJson() method to retrieve profile information

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

Upvotes: 1

Related Questions