If you are interested in automated testing web applications with Selenium or Webdriver but you prefer python over Java this getting started guide can help you with basic steps and setup.
The first step is to install python. You can download it from the official site (https://www.python.org/downloads/) or if you use Linux install it from the repositories.

For Debian / Ubuntu users:

$ sudo apt-get install python

For RedHat:

$ sudo yum install python


# yum install python

If you are not sure about which version of python is installed or if you have any of them on your machine just check it out on console / terminal typing the following command:

python –-version

The next step is to install Webdriver for python. The easiest way is using pip for installation (python package management system). In Python 3.4 pip is included by default but for older versions you have to install it manually (https://pip.pypa.io/en/latest/installing.html#install-pip). After that the installation of Selenium looks like:

pip install selenium

You are ready to create your test case. Here is an example which presents the basics.

from selenium import webdriver
from selenium.webdriver.common.by imp ort By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

def waitForElementToAppear(by, locator):
return wait.until(expected_conditions.visibility_of_element_located((by, locator)))
except TimeoutError:
print("Element is not appeared")

driver = webdriver.Firefox()

if not "Google" in driver.title:
raise Exception("Unable to load Google page!")

webelement = driver.find_element_by_name("q")

topResult = waitForElementToAppear(By.CSS_SELECTOR, "ol>li.g .rc h3 a")

if not "wedoqa" in driver.title:
raise Exception("Result page is not loaded")

print("The top result is: " + topResult.text)


To run the test above create a file with .py extension (for instance test.py) and copy the code into the file. If this is finished open a console or terminal and navigate to the folder containing the file which was created before. Run the test with the following command:

python yourFile.py

The expected result is: “The top result is: WeDoQA”
This test is compatible with python 3 so if you experience some trouble with running it check your python version.
Documentation and useful information is available on the official selenium – python site: https://selenium-python.readthedocs.org/

Posted By Árpád Farkas

    2 Responses to “Getting started with Webdriver python guide”

  1. Lucky says:

    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys

    class SearchTests(unittest.TestCase):

    def setup(self):

    # create a new Firefox session
    self.driver = webdriver.Chrome(“C:\\Users\\Dreamthought\\Downloads\\chromedriver_win32”)

    I am getting a bunch of error message while running the above code. Please help me to fix the issue.

    C:\Users\Dreamthought\AppData\Local\Programs\Python\Python35-32\python.exe “C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.1\helpers\pycharm\utrunner.py” C:\Users\Dreamthought\PycharmProjects\Myfirstselenium\MyPythonProgram\Basicscript.py true
    Testing started at 7:56 AM …
    Traceback (most recent call last):
    File “C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.1\helpers\pycharm\utrunner.py”, line 153, in
    modules = [loadSource(a[0])]
    File “C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.1\helpers\pycharm\utrunner.py”, line 65, in loadSource
    module = imp.load_source(moduleName, fileName)
    File “C:\Users\Dreamthought\AppData\Local\Programs\Python\Python35-32\lib\imp.py”, line 172, in load_source
    module = _load(spec)
    File “”, line 693, in _load
    File “”, line 673, in _load_unlocked
    File “”, line 661, in exec_module
    File “”, line 767, in get_code
    File “”, line 727, in source_to_code
    File “”, line 222, in _call_with_frames_removed
    File “C:\Users\Dreamthought\PycharmProjects\Myfirstselenium\MyPythonProgram\Basicscript.py”, line 7
    def setup(self):
    IndentationError: expected an indented block

    Process finished with exit code 1

    • Hello Lucky,

      As the error message says you are having problems with indentation in line 7 of you code. Make sure that you have the correct indentation(tab or 4 spaces) after def setup(self):

      def setup(self):
          # create a new Firefox session
          self.driver = webdriver.Chrome(“C:\Users\Dreamthought\Downloads\chromedriver_win32”)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>