Search code examples
pythonpython-3.xgoogle-chromeselenium-webdriverselenium-chromedriver

Can you use Python3/Selenium to open my browser with plugins?


I have plug ins with login credentials/API keys I want to be using in the Chrome browser when python/selenium opens a new window.

However, currently when my code opens the browser, it's a new Chrome window without any extensions.

How do I get it to point towards opening the default browser, as if I was opening Chrome?

My code

from selenium import webdriver
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.common.by import By
import time

def perform_login_logout(url, username, password, new_url):
    # Set the path to your Chrome driver executable


    # Create an instance of Chrome WebDriver
    driver = webdriver.Chrome()

    # Open the URL to the login page
    driver.get(url)

Solution

  • It can be done by setting the user_data_dir via options.

    options.add_argument("user-data-dir=%s" % path_to_user_data_dir)

    A full example:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    service = Service()
    options = webdriver.ChromeOptions()
    options.add_argument("user-data-dir=%s" % path_to_user_data_dir)
    driver = webdriver.Chrome(service=service, options=options)
    # ...
    driver.quit()