Saturday, November 5, 2016

Selenium WebDriver use a Specific Profile with ChromeDriver to Save Login Session and Cookies

I've been looking all over the internet to find a way to login to a website with Selenium and keep the cookies saved. So I won't have to relogin every time. But all I got was disappointment. Then I came across the Google Chrome Driver. It has command line options to just what I was trying to do. I just want to share it with you guys. I know it's a pain in the ass to fix things like this.

System.setProperty("webdriver.chrome.driver", "driver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments(String.format("user-data-dir=%s", "profile\\dir"));
options.addArguments(String.format("profile-directory=%s", "User"));

WebDriver driver = new ChromeDriver(options);

That's how I initialized the ChromeDriver and it worked like a charm. Do remember that, profile\\dir is the directory where you want to save the google chrome profile data. User is the username for which you would like to save the preference of. Usually a directory will be created inside the user-data-dir with "User", where all the user specific configurations will be saved.

No comments:

Post a Comment