How to Open Chrome using Selenium in Python

Deepanshu Bhalla Add Comment

This tutorial explains the steps to open Google Chrome using Selenium in Python.

  1. Install the Selenium library in Python using the command below -
    pip install selenium
  2. Download the chrome webdriver from ChromeDriver Website as per your Chrome browser version and operating system. To check the version of your Chrome browser, either type chrome://version/ in the URL of your chrome browser. OR You can click on the three-dot icon in the top right corner of the chrome browser and then go to "Help" and then select "About Google Chrome".
  3. Specify the path in the code below and make sure to use either forward slash or double backslash (\\) in Windows OS -
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    # Make sure it is .exe file
    PATH = (r"C:/Users/deepa/Downloads/chromedriver-win64/chromedriver-win64/chromedriver.exe")
    service = Service(executable_path=PATH) 
    driver = webdriver.Chrome(service=service)
    
    # Open a website
    driver.get('https://www.wikipedia.com')
    
    # Close the browser
    driver.quit()
    

How to Open Chrome with Your Login using Selenium

To open Chrome with your login credentials using Selenium, follow the steps below. This is really helpful when you need to login to the site or access cookies while web scraping or testing the site.

  1. Type chrome://version/ in the URL of your browser.
  2. Check Profile Path as highlighted in the image below. Copy the path. Launching Chrome using Selenium
  3. If the last folder of the path is 'Default', put it in the --profile-directory below and the remaining path in the --user-data-dir.
    options.add_argument("--user-data-dir=C:\\Users\\deepa\\AppData\\Local\\Google\\Chrome\\User Data")
    options.add_argument('--profile-directory=Default')
    
  4. Close the browser if it is already opened before running the code below.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("--log-level=3")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--user-data-dir=C:\\Users\\deepa\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument('--profile-directory=Default')
options.headless = False

PATH = (r"C:/Users/deepa/Downloads/chromedriver-win64/chromedriver-win64/chromedriver.exe")
service = Service(executable_path=PATH) 

# Make sure CHROME IS CLOSED BEFORE RUNNING THIS
driver = webdriver.Chrome(service=service, options=options)

# Open a website
driver.get('https://www.wikipedia.com')

# Close the browser
driver.quit()
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Open Chrome using Selenium in Python"
Next → ← Prev