Getting started with Selenium Webdriver in Python

Python is a object-oriented programming which has simple syntax, making it the easy for someone trying to learn programming. Writing programs in Python both fun and easy.

Selenium automates browsers. Selenium is an open source library which can used to perform testing web applications. Selenium works multiple browsers Chrome, Firefox, IE, Edge, Safari etc.

To start with Selenium Webdriver Python tutorial, lets install Python and Selenium before starting.

If you don't already have Python, you can install it like described by Python installed guide. Please download python and install on your machine.

Once you've got it installed, In the command prompt window, type "python" and press Enter. If you see a response from a Python interpreter it will include a version number in its initial display.

Python bindings for Selenium

You can download selenium bindings using pip. Latest versions of Python already comes with pip module in the standard library.

Open command prompt and install selenium by typing "pip install selenium" and press enter. Here pip is a command line program. When you install pip, a pip command is added to your system.

pip install selenium

Wait Wait... We're Not Done Yet!

Selenium requires a driver to perform operations. Different browsers require different drivers. For example, Google Chrome requires "chrome driver". Similarly every browser requires their specific drivers. Here are the links of some of the popular drivers.

These drivers will come in the form of an executable (Windows) or a binary (Mac/Unix). You can get the latest release of required drivers from the following.

You can download browser drivers from below links :





Let's get started writing Selenium code in Python

from selenium import webdriver
# if chromedriver is not in your path, you’ll need to add it here
driver = webdriver.Chrome(r'C:\Python\chromedriver.exe')

When you run the above script, it will just launch chrome browser.

For Windows, once you download a zip file, extract it and add the .exe file to your PATH and for Linux or Mac OS, extract the downloaded file in a directory add it to the PATH (e.g. /usr/bin). After adding the path then just call the default constructor webdriver.Chrome()

To launch the above script on firefox browser, add the below :-

from selenium import webdriver
driver = webdriver.Firefox(executable_path='C:\Python\geckodriver.exe')

If we execute the above script without "executable_path", it gives an "NotADirectoryError".
Selenium client bindings tries to locate the geckodriver executable from the system PATH.

We need to pass the Key "executable_path" along with the value referring to the absolute path of the GeckoDriver.

Selenium Tutorials: 

Add new comment