Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run Selenium ChromeDriver with cookies from actual Chrome installation?

So I'm running a selenium test using IntelliJ IDEA + chromedriver on an Ubuntu machine...

In my Google Chrome installation, I have logged in to an account, say Google. When I access http://accounts.google.com in the selenium test, I get to the log in page instead of the actual account management page.

I'm pretty sure that I don't fully understand the exact way that Selenium and the chrome driver operate, but I do remember that having 'Google Chrome installed in the default location' is one of the requirements of running a Selenium test with the chrome driver.

Can I run in the context of my installed browser i.e. have access to my browser history and cookies?

like image 709
Satisfakshin Avatar asked Dec 09 '15 12:12

Satisfakshin


People also ask

Do I need Chrome installed to use ChromeDriver?

Users provided relevant link to confirm that, "YES" a full Chrome installation is needed in addition to the actual chromedriver.

Does ChromeDriver need to match Chrome version?

Specifically: ChromeDriver uses the same version number scheme as Chrome. See https://www.chromium.org/developers/version-numbers for more details. Each version of ChromeDriver supports Chrome with matching major, minor, and build version numbers.

Can Selenium use cookies?

Selenium cookies can be extensively used for test automation projects for performing cookie-related operations during the process of Selenium automation testing. To realize this requirement, Selenium WebDriver API provides built-in methods for interacting with the cookies.

Do we need to install ChromeDriver for Selenium?

As Google Chrome dominates the browser market, the use of a ChromeDriver becomes a must. Selenium WebDriver uses the ChromeDriver to communicate test scripts with Google Chrome.


2 Answers

Everytime Selenium opens a browser (Chrome/Firefox/IE) it opens a canonical form of that browser. As a tester, you can set browser preferences using DesiredCapabilities object and for chrome you also can use ChromeOptions object, for passing chrome command line arguments.

To choose your profile

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");

For more on chrome driver capabilities: https://sites.google.com/a/chromium.org/chromedriver/capabilities

For more about user-data-dir command line option for chrome:
https://www.chromium.org/user-experience/user-data-directory

like image 106
parishodak Avatar answered Oct 10 '22 17:10

parishodak


Try this:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

To find path to your chrome profile data you need to type chrome://version/ into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default, to use it in the script I had to exclude \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.

Source:How to load default profile in chrome using Python Selenium Webdriver?

like image 31
benjamin deworsop Avatar answered Oct 10 '22 16:10

benjamin deworsop