Python Selenium – Open Chrome
In this tutorial, you shall learn how to open Google Chrome browser, using Selenium, in Python language.
We shall use webdriver_manager for creating a driver object for the Chrome. If you do not have webdriver_manager installed, open a terminal or command prompt, and run the following pip command.
pip install webdriver-manager
Steps to open Chrome browser using Selenium Python
The first step in creating a Chrome driver instance is to import ChromeDriverManager class from the webdriver_manager.chrome module.
ChromeDriverManager class instance identifies the required webdriver package for the version of Chrome installed in your system.
The webdriver package is fetched dynamically from the predefined online repository, based on your Operating System and the version of Chrome present in your system.
Import Service class from chrome.service module present in selenium.webdriver. This shall be used for the service parameter of webdriver.Chrome().
Create a Chrome webdriver instance using webdriver.Chrome class.
You do not have to remember these steps. Just copy the first six lines of code from the following example, and everything should be fine to create a Chrome webdriver instance and open a Chrome browser window.
In the following example, we followed the above said steps to create a Chrome webdriver instance, and open a Chrome browser window.
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService # Initialize Chrome driver instance driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install())) # Navigate to the url driver.get('https://pythonexamples.org/tmp/selenium/index.html') # Close the driver driver.quit()
In this Python Selenium Tutorial, we have seen how to create a Chrome webdriver instance and open a Chrome browser window.