How to log in to Chrome with Selenium?

ThisIsNoZaku picture ThisIsNoZaku · May 1, 2017 · Viewed 14.3k times · Source

I am testing a Chrome extension which requires the user to be logged in to use, but I cannot figure out how to login with my test account. I have tried logging in to accounts.google.com but this is apparently insufficient; as far as the chrome APIs are concerned there is no authenticated user.

Chrome keeps prompting for login at chrome://chrome-signin but because I can't view the html of the page I can't determine what elements to interact with in Selenium to use it.

Answer

Shailendra picture Shailendra · May 1, 2017

You may need to login manually once and then use that for automation. Try below code , may be it help you:

System.setProperty("webdriver.chrome.driver","<chrome exe path>");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir= <full local path Google Chrome user data default folder>);

WebDriver driver = new ChromeDriver(options);
driver.get("https://mail.google.com");

Login once manually when browser launched.

Then re-run script now it should use previous login.

Hope it will help you.