Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access my existing cookies using Chrome?

I have cookies with Gmail login info, so that chrome automatically opens my Gmail.

I tried the following code, but it didn't work:

    System.setProperty("webdriver.chrome.driver","chromedriver\\chromedriver.exe"); 
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setCapability("chrome.switches", Arrays.asList("C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default"));
//I also tried using: capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir = C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default"));

            WebDriver driver = new ChromeDriver(capabilities);

            driver.get("https://gmail.com");

I checked the directory of C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default it is okay. What is the problem in here?

like image 899
Buras Avatar asked Jan 13 '23 21:01

Buras


1 Answers

There is a Known issues section on the Chrome Driver official wiki page I haven't noticed before:

Known Issues

3 . Cannot specify a custom profile

Now, I don't know whether this is or isn't outdated. I could not find a bug report for this. It's true that you can't specify a custom profile via Capabilities (as of July 2013), as you discovered. But there is a solution...


The Solution

Here's how I managed to make it run:

ChromeOptions opt = new ChromeOptions();
opt.setBinary("E:\\some\\path\\chrome.exe");
opt.addArguments("--user-data-dir=C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(opt);

Notice the path to the User data directory - it does not have the \\Default part. And in that case, it works just fine for me, opens up the Chrome profile stored with all the cookies and logins.

I have no idea why the Capabilities solution does not work. It might be worthwile to file a bug as I could not find one on topic.

like image 191
Petr Janeček Avatar answered Jan 16 '23 00:01

Petr Janeček