Setup Selenium for Python

Setup Selenium

1. Install Selenium Libraries

To install Selenium libraries for Python programming, run the following command.

pip install selenium
Setup Selenium for Python

2. Download Browser Drivers

We can download browser drivers from the respective repositories. The links are given below.

BrowserSupported OSLink
Chromium/ChromeWindows/macOS/LinuxDownloads
FirefoxWindows/macOS/LinuxDownloads
EdgeWindows/macOSDownloads
Internet ExplorerWindowsDownloads
SafarimacOS High Sierra and newerBuilt in to Safari browser

Download the browser driver based on the browser version in your PC/Mac.

For example, I would like to find out which driver I should download to use it my Mac for Chrome browser. So, I open Chrome browser and find out the version.

Setup Selenium for Python

As you may have observed, the version is 103.0.5060.53. Therefore, we have to find this version in Chrome browser driver downloads page.

Setup Selenium for Python

I am working on Mac, therefore, I download the chrome driver for Mac that is highlighted in the following screenshot.

Setup Selenium for Python

So, I download this driver that respects my Chrome version. You have to download the version that respects your browser and its version.

3. Make Driver Visible to Python Program

There are three ways in which we can make the driver visible to our Python code.

In this section, we will present two ways.

  • Using PATH environment variable.
  • Specifying hardcoded location in the Python program.

Once the respective driver is downloaded, you may add it the PATH based on your Operating System, or move it to a location and use that location hardcoded in the program.

I extracted the driver from zip file, and moved it the location /usr/local/bin/.

Since our tutorials are for learning purpose, we specify hardcoded location in the Python program, as shown in the following.

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

service = Service(executable_path="/usr/local/bin/chromedriver")
driver = webdriver.Chrome(service=service)

4. Selenium First Program

Now, we write our first program, to start Web Driver and open the url "https://www.google.com/".

Python Program

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

service = Service(executable_path="/usr/local/bin/chromedriver")
with webdriver.Chrome(service=service) as driver:
    driver.get('https://www.google.com/')
Copy

If you are working in Mac, and get a waring popup message that says ““chromedriver” cannot be opened because the developer cannot be verified.”, you may execute the following command.

xattr -d com.apple.quarantine /usr/local/bin/chromedriver
Copy

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q3. What is the method used to take a screenshot in Selenium Python?

Not answered

Q4. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍