How to install Selenium on Linux and automate your web tests

How to  install Selenium on Linux and automate your web tests

Selenium is a web testing toolkit that allows you to test a web application in any browser of your choice. It does this with the help of a software called WebDriver which allows you to emulate a web browser and test your web application in it.

In this post I am going to show you how to test a simple CRUD application, by automating link clicking and form filling.

Software versions
Selenium (2.44.0)
Python 3.4.2
Linux kernel 3.13.0-37-generic and Mint 17.1 for Desktop.

Installing Selenium on Linux
To install the Selenium Bindings for Python on Linux use pip.

[leo@selenium-on-linux]$ virtualenv -p /usr/bin/python3 venv-3.4
[leo@selenium-on-linux]$ source venv-3.4/bin/activate
[leo@selenium-on-linux]$ pip3 install selenium

The webdriver for Firefox is installed along with Selenium but if you want to test your applications on chrome then you need to download the chrome driver and ensure chrome is installed on your Linux distro.

Getting started with the code.
The first thing you need to do is create an instance of the webdriver.

[leo@selenium-on-linux]$gedit selenium-tests.py
from selenium import webdriver

driver=webdriver.Chrome('/home/leo/Downloads/chromedriver')

Next you need to specify the url of the web application you need to test. I will be using a custom CMS application which is used to create, read, update and delete posts for this demonstration. Below is the screenshot of the app.

Install Selenium on Linux

driver.get("http://url-for-posts.com")
assert "Post Page Title" in driver.title

The above code will fetch the web page and check if the web page has been successfully fetched by matching the content in the page title.

Now let’s test the “Create”, i.e add a post.

  link=driver.find_element_by_link_text("Add new")
  NewWindow=link.click()
  assert "Save" in driver.page_source

In the code above I use “find_element_by_link_text()” to perform actions on the anchor HTML element which contains the text “Add new”. You can get elements with their ID or name or even by class name. You can click on elements with the “click()” method.

On clicking the “Add new” link you get the page to create a post.

Install Selenium on Linux

Here we need to fill in the title, body, category, select whether the post will be published and then click on the “Save” button to add the post.

So let’s fill in the form with some text

#Add posts
        title=driver.find_element_by_name("title")
        title.send_keys("Your Title text here")

        content=driver.find_element_by_name("content")
        content.send_keys("Your Body text here")

        category=driver.find_element_by_name("category")
        category.send_keys("Your category text here")

Below is the html of the form elements.


As I mention earlier you can fetch an element by it’s name with “find_element_by_name(“element name”)”. You can then perform an action on it like fill it with text using “send_keys()”.

For the “select” element we will need to import the “Select” class.

from selenium.webdriver.support.ui import Select

 select = Select(driver.find_element_by_name('published'))
 published=select.select_by_value("Your option value")

Now we need to click on Save, however the “Save” button does not have a name attribute hence we need to use the class name.

 button=driver.find_element_by_class_name('button')
 success=button.click()
 assert "Add was successful" in driver.page_source

Similarly the code for update is

 #Edit Posts
        link=driver.find_element_by_link_text("Edit")
        newindow=link.click()
        assert "Save" in driver.page_source

        title=driver.find_element_by_name("title")
        title.send_keys("Selenium web test edit")
        content=driver.find_element_by_name("content")
        content.send_keys("Selenium web test edit")
        category=driver.find_element_by_name("category")
        category.send_keys("Selenium web test edit")

        select = Select(driver.find_element_by_name('published'))
        published=select.select_by_value("0")

        button=driver.find_element_by_class_name('button')
        success=button.click()
        assert "Update was successful" in driver.page_source

And the code for delete is

 #Delete Posts
 link=driver.find_element_by_link_text("Delete")
 NewWindow=link.click()
 assert "Post was deleted successfully" in driver.page_source

The complete source code is available at https://github.com/Leo-g/Selenium/blob/master/python-selenium.py

I have also written a scaffolding app which will automatically creates tests for your modules along with the CRUD modules, you can download it from https://github.com/Leo-G/Flask-Scaffold and read about it at Building database driven apps in Python with Flask

You can view the code in action in the video below.

Comments and feedback is welcome.

Ref (http://selenium-python.readthedocs.org/en/latest/getting-started.html#using-selenium-to-write-tests)

Follow me

Leo G

Is a Linux Hobbyist and Enthusiast. He Strongly believes in OpenSource Software and would like you to view and download his software at https://github.com/Leo-g
Follow me

  • Pingback: How to install Selenium on Linux and automate y...()

  • Pingback: How to install Selenium on Linux and test your web applications | Linux Press()

  • Pingback: How to install Selenium on Linux and test your web applications | Zbyněk Svoboda()

  • Pingback: Links 15/3/2015: Linux Lite 2.4, OpenELEC 5.0.6 | Techrights()

  • jwebuser

    Great, thank you for this post!

    • Leo G

      You welcome

  • White Ninja

    Good article with step by step explanation. I am web scraper ( here is my website to look at : http://ur1.ca/ofzyt ) and using selenium for my web scraping work since last 6 years.

  • ScraperHunk

    Selenium is handy when a particular website is protected by anti web scraping technology. Here is my website to look at : http://ur1.ca/olzum

  • Silambu Mani

    Exception in thread “main” java.lang.UnsupportedClassVersionError: org/openqa/selenium/WebDriver : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
    at java.lang.Class.getMethod0(Class.java:2866)
    at java.lang.Class.getMethod(Class.java:1676)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

    hai i am running webdriver code eclipse in linux 14.04 LTS showing the error like console how to clear the error..