This tutorial explains the steps to open Google Chrome using Selenium in Python.
- Install the Selenium library in Python using the command below -
pip install selenium
- 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". - 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.
- Type
chrome://version/
in the URL of your browser. - Check Profile Path as highlighted in the image below. Copy the path.
- 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')
- 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()
Share Share Tweet