1

I would like to use an existing installation of chrome (or firefox or brave browser) with selenium. Like that I could set prespecified settings / extensions (e.g. start nord-vpn when opening a new instance) that are active when the browser is opened with selenium. I know there is selenium.webdriver.service with the "executeable-path" option, but it doesn't seem to work when you specify a specific chrome.exe, the usage seems to be for the chrome-driver only and then it still opens a "fresh" installation of chrome.

Starting selenium with extension-file I think is also not an option to use with the nord-vpn extension, as I have two-factor authentication active and login every single time would take too much time and effort, if possible at all.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
kayB
  • 151
  • 2
  • 9

1 Answers1

1

Firefox profile

To use the existing installation of you have to pass the profile path through set_preference() method using an instance of Option from selenium.webdriver.common.options as follows:

from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")

You can find a relevant detailed discussion in Error update preferences in Firefox profile: 'Options' object has no attribute 'update_preferences'


Chrome profile

Where as to use an existing installation of you have to pass the user profile path through add_argument() using the user-data-dir key through an instance of Option from selenium.webdriver.common.options as follows:

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

options = Options()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.com/")

You can find a relevant detailed discussion in How to open a Chrome Profile through Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Do I need to use a specific geckodriver (linked to my firefox installation)? With: `option = ffOpt() profile_path = r'C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\xyz123.default-release' option.set_preference('profile', profile_path) service = Service('C:\\BrowserDrivers\\geckodriver.exe') driver = Firefox(service=service, options=option) driver.get("https://www.google.com")` the regular fresh and clean firefox opens without any extensions etc. installed. – kayB Jan 17 '23 at 07:15
  • For the Chrome solutions I'm struggling currently with the chromedriver.exe, that's why originally I was using the `chromedriver_autoinstaller.install()`. So the questions is again, does the driver matter in a sense that it needs to match for example the version of my browser? If I'm running it with `chromedriver_autoinstaller.install()` again a fresh and clean installation is opened. – kayB Jan 17 '23 at 07:26
  • @kayB Ofcoarse, version of matching **geckodriver** is needed to run the execution. See the [compatibility chart](https://stackoverflow.com/a/45331403/7429447). To add an extension see [How to install extension permanently in geckodriver](https://stackoverflow.com/a/54807581/7429447) – undetected Selenium Jan 17 '23 at 10:17
  • @kayB For the **Chrome** solutions `chromedriver_autoinstaller.install()` should also work seamless as it downloads the latest [ChromeDriver](https://stackoverflow.com/a/59927747/7429447). To add an extension see [How to install Chrome Extension using Selenium & Python](https://stackoverflow.com/a/59956655/7429447) – undetected Selenium Jan 17 '23 at 10:20
  • I now manage with `option.add_argument('--profile-directory=Profile 2')` after creating an extra profile called "Profile 2" and `option.add_argument('user-data-dir=C:\\Users\\...\\Chrome\\User Data\\')` that the specific profile is opened. With nordvpn up and running as soon as chrome instance is opened (what I want to achieve), but then the chromedriver.exe crashes: "C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)" and "unknown error: DevToolsActivePort file doesn't exist" – kayB Jan 17 '23 at 13:48
  • @kayB Sounds like a different question all together. Feel freeto raise a new question with your new requirement. – undetected Selenium Jan 17 '23 at 13:53