Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: Point towards default Chrome session

Though I realize it's NOT "good" practice - I have a use case where I need to point (hook up) the Selenium driver to my default Chrome session/profile.

My default profile is here: ~/Library/Caches/Google/Chrome/Default

Here is how I'm seting it up currently: (not working)

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Caches/Google/Chrome")
options.add_argument("--profile-directory=Default")
browser = webdriver.Chrome(options=options, executable_path=r"./chromedriver")
browser.get("http://google.com")

I'm using Chrome version 74.0.3729.169 and chromedriver version ChromeDriver 74.0.3729.6 (which is the compatible version).

When Chrome opens I don't see any cookies in Chrome's settings so it's clear it's NOT being pointed to my default session. Also, I see that a Selenium directory has been created (which appears to mean that it has failed to connect to the session at ~/Library/Caches/Google/Chrome/Default.

How do I hook up selenium to my default Chrome session? This is the same session as one sees when normally opening up Chrome.

I've looked at this other question, but the answer there fails to address how to point Selenium towards default session. Also - it's an outdated question - Chrome and Chromedriver have progressed a lot since then. Also, the question there assumes that the poster is able to connect to default session - I am not able to do that which suggests that the Chromedriver/Chrome have changed since then. Also that question is for Windows - I'm on a Mac where things work differently.

like image 639
etayluz Avatar asked May 28 '19 14:05

etayluz


People also ask

How do I make Chrome my default in Selenium?

We can open Chrome default profile with Selenium. To get the Chrome profile path, we need to input chrome://version/ in the Chrome browser and then press enter. We need to use the ChromeOptions class to open the default Chrome profile. We need to use the add_argument method to specify the path of the Chrome profile.

Can Selenium interact with an existing browser session?

We can interact with an existing browser session. This is performed by using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

What is ChromeOptions Selenium?

ChromeOptions is a new concept added in Selenium WebDriver starting from Selenium version 3.6. 0 which is used for customizing the ChromeDriver session. By default when selenium opens up any browser (Chrome browser or Firefox browser), it opens up without any extension or history or cookies, etc.


2 Answers

To start with, No, you can't point (hook up) the Selenium driver to any of the existing/previous Web Browsing session. Even if you are able to extract the Session ID, Cookies and other session attributes from the existing/previous Web Browsing session, still you won't be able to pass those attributes as a HOOK to the WebDriver.

You can find a detailed discussion in How can I reconnect to the browser opened by webdriver with selenium?

But of coarse you can connect to the existing Default Chrome profile.


You seem to be already aware that trying to use the Default Chrome Profile for Test Automation will be against all the best practices as the Default Chrome Profile may contain either/all of the following:

  • browser settings
  • Extensions
  • Bookmarks
  • Apps
  • Saved Passwords
  • Browsing History
  • etc

So the Default Chrome Profile may not be in compliance with you Test Specification and may occasionally raise exception while trying to load. Hence you should always use a customized Chrome Profile.

You can find a detailed discussion in How to open a Chrome Profile through --user-data-dir argument of Selenium

If your usecase still warrants to use the Default Chrome Profile you need to follow the below mentioned details.


Location of Default Chrome Profile

As per the documentation in How to Find Your Chrome Profile Folder on Windows, Mac, and Linux the location for Chrome’s default profile folder differs depending on your platform. The locations are:

  • Windows 7, 8.1, and 10: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
  • Mac OS X El Capitan: Users/<username>/Library/Application Support/Google/Chrome/Default
  • Linux: /home/<username>/.config/google-chrome/default

You need to replace <username> with the name of your user folder. The default profile folder is simply named Default (or default in Linux). However, if you’ve created additional profiles, their folder names are not as obvious. The name you assigned to the profile when you created it displays on a name button on the right side of the title bar on the Chrome window. Unfortunately, the name Chrome uses on the associated profile folder is a generic, numbered name like Profile 3.

If you need to know any of the Chrome Profile's folder name, you simply need to access chrome://version in the address bar and press Enter.

Snapshot:

DefaultChromeProfile

The Profile Path shows the location of the current profile. For example, the location of my Default profile in my Windows 10 system is C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\Default. You can select the path and copy it and paste it into File Explorer in Windows, the Finder on OS X or into a file manager like Nautilus in Linux to access that folder.


Sample Code (Windows 10)

Finally, to access the Default Chrome Profile you can use the following Python based solution:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\Soma Bhattacharjee\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

You can find a detailed discussion in How to use Chrome Profile in Selenium Webdriver Python 3

like image 150
undetected Selenium Avatar answered Nov 14 '22 02:11

undetected Selenium


Make sure you are pointing to the right folder using "Chrome://version".

enter image description here

I am using the windows but it should be similar in you mac case too.

Refer to this link for more information.

How to create a custom profile:

You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the user-data-dir switch set to some new directory. If the path doesn't exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open chrome://version in the browser to see what profile Chrome is using.

Reference:

http://chromedriver.chromium.org/capabilities

like image 29
supputuri Avatar answered Nov 14 '22 03:11

supputuri