Selenium Webdriver with Python: Tutorial with Example

Selenium supports Python and thus can be utilized as Selenium WebDriver with Python for testing.

  • Python is easy compared to other programming languages, having far less verbose.
  • The Python APIs empower you to connect with the browser through Selenium.
  • Selenium sends the standard Python commands to different browsers, despite variation in their browser’s design.

You can run Selenium with Python scripts for Firefox, Chrome, IE, etc. on different Operating Systems.

In this Selenium Python tutorial, you will learn-

What is Python?

Python is a high-level object-oriented scripting language. It is designed in a user-friendly manner. Python uses simple English keywords, which is easy to interpret. It has less syntax complications than any other programming languages.

See some of the examples in the table below.

Keyword Meaning Usage
elif Else if Else if
else Else if: X; elif: Y; else: J
except do this ,If an exception happens, except ValueError, a: print a
exec Run string as Python exec ‘print “hello world !”‘

What is Selenium?

Selenium is a tool to test your web application. You can do this in various ways, for instance

  • Permit it to tap on buttons
  • Enter content in structures
  • Skim your site to check whether everything is “OK” and so on.

Why to choose Python over Java in Selenium

Few points that favor Python over Java to use with Selenium is,

  1. Java programs tend to run slower compared to Python programs.
  2. Java uses traditional braces to start and ends blocks, while Python uses indentation.
  3. Java employs static typing, while Python is dynamically typed.
  4. Python is simpler and more compact compared to Java.

How to Install and Configure PyDev in Eclipse

PyDev is Python development environment for Eclipse.

Step 1) Got to Eclipse Marketplace. Help > Install New Software

The next step is to install “pydev IDE” for eclipse.

Step 2) In this step,

  1. Search for “” in Work with and then
  2. Select all listed Items and click on Next twice
  3. Accept the Licence Agreement and click Finish.

Step 3) You may encounter Security Warning, Click on “Install Anyway”.

Step 4) Now, in this step you will set preferences. With the help of preference option, you can use Python as per the project need.

Go to Window > Preferences > PyDev > Interpreter > Python Interpreter.

Let’s sets the default Python Interpreter. It is just like you need to set java compiler for running a Java code. To change the interpreter name, click on Browse for python/pypy exe Button.

Step 5) In this step, give the “interpreter name” and the “exe” file path of Python.

  1. Click on ‘Browse’ and find python.exe where you installed Python.
  2. Click ‘OK’ button.
  3. Select all the Folder and click on OK
  4. Click on “Apply and Close”.

Step 6) Make a New Project in Python. In this step,

  1. Right click PyDev Package Explorer > New.
  2. Select option others.

  1. Select “PyDev > PyDev Project”.
  2. Press ‘Next’ button.

  1. Name your Project
  2. Click “Finish”.

You can see the new Python (PyDev) project is created.

Step 7) In this step,

After creating ‘PyDev Project’, you will create a new Python package.

  1. Right click on Project > New > PyDev Package.
  2. Name your Package and Click Finish.

Step 8) If you see in below screenshot, a new package is created.

After creating a new package, the next step is to createPyDev Module. The module contains some Python files for initialization. These files or functions from the module can be imported into other module. So, there will be no need to re-write the program again.

Step 9) Createa new PyDev module. Right click on package > New > PyDev module.

Name your Module and click “Finish”.

Select Empty Template and Click “OK”.

Step 10) Write your code for Selenium with Python as shown below

How to Create Test Scripts in Selenium with Python

In this Selenium WebDriver with Python example, we did automation for “Facebook login page” using the Firefox driver.

Selenium Python Example 1: Login into Facebook

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
user_name = "YOUR EMAILID"
password = "YOUR PASSWORD"
driver = webdriver.Firefox()
element = driver.find_element_by_id("email")
element = driver.find_element_by_id("pass")

Snapshot of the Code

Explanation of the code

  • Code line 1: From selenium module import webdriver
  • Code line 2: From selenium module import Keys
  • Code line 3: User is a variable which will be we used to store values of username.
  • Code line 4: Variable “password” will be used to store values of the password.
  • Code line 5: In this line, we are initializing “FireFox” by making an object of it.
  • Code line 6: The “driver.get method” will navigate to a page given by the URL. WebDriver will wait until the page has been completely loaded (that is, the “onload” occasion has let go), before returning control to your test or script.
  • Code line 7: In this line, we are finding the element of the textbox where the “email” has to be written.
  • Code line 8: Now we are sending the values to the email section
  • Code line 9: Same for the password
  • Code line 10: Sending values to the password section
  • Code line 11: element.send_keys(Keys.RETURN) is used to press enter after the values are inserted
  • Code line 12: Close


The values of the username “guru99” and password entered.

The Facebook page will login with email and password. Page opened (see image below)

EXAMPLE 2: Login into Facebook & Check Title

In this example,

  • We will open a login page.
  • Fill the required field “username” and “password”.
  • Check Page Title
from selenium import webdriver
from import WebDriverWait
# Step 1) Open Firefox 
browser = webdriver.Firefox()
# Step 2) Navigate to Facebook
# Step 3) Search & Enter the Email or Phone field & Enter Password
username = browser.find_element_by_id("email")
password = browser.find_element_by_id("pass")
submit   = browser.find_element_by_id("loginbutton")
username.send_keys("YOUR EMAILID")
password.send_keys("YOUR PASSWORD")
# Step 4) Click Login
wait = WebDriverWait( browser, 5 )
page_title = browser.title
assert page_title == "Facebook"

Snapshot of the code

Explanation of the code:

  • Code line 1-2: Import selenium packages
  • Code line 4: Initialize Firefox by creating an object
  • Code line 6: Get login page (Facebook)
  • Code line 8-10: Fetch username, password input boxes and submit button.
  • Code line 11-12: Enter data into username and password input boxes
  • Code line 14: Click on the “Submit” button
  • Code line 15: Create wait object with a timeout of 5 sec.
  • Code line 16: Capturing the title from “browser” Object.
  • Code Line 17: Testing the captured title string with “Facebook”


  • Selenium is an open-source web-based automation tool.
  • Python language is used with Selenium for testing. It has far less verbose and easy to use than any other programming language
  • The Python APIs empower you to connect with the browser through Selenium
  • Selenium can send the standard Python commands to different browsers, despite variation in their browser’s design.