How to access existing cookies using Chrome?

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

I tried the following code but it did not 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 C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default , this is normal. What is the problem?

+4
source share
1 answer

There are Known Issues on the official Chrome Driver wiki page, which I had not noticed before:

Known Issues

3. Unable to specify custom profile

Now I do not know whether or not it is out of date. I could not find the error report. It is true that you cannot specify a custom profile through Capabilities (as of July 2013), as you discovered. But there is a solution ...


Decision

Here's how I managed to run it:

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

Pay attention to the path to the user data directory - it does not have the \\Default part. And in this case, it is great for me, it opens a Chrome profile stored with all cookies and logins.

I have no idea why the Capabilities solution is not working. Perhaps it would be to write down the error , because I could not find it on the topic.

+9
source

Source: https://habr.com/ru/post/1491574/


All Articles