In web de­vel­op­ment nowadays, it’s ab­solute­ly essential to prepare an ap­pli­ca­tion for the different display sizes of users or visitors. The re­spon­sive approach involves a published web project to au­to­mat­i­cal­ly adjust to the technical re­quire­ments of the re­spec­tive device and has es­tab­lished itself as the un­con­test­ed standard solution. Just as important for modern websites as high flex­i­bil­i­ty regarding display size is optimal browser com­pat­i­bil­i­ty: for the end users, it should make no dif­fer­ence if they access a site with Firefox, Chrome, Safari or different web client.

It’s not sur­pris­ing then that cross-browser testing has played a major role in pro­gram­ming web projects for many years. One of the most popular tools for per­form­ing these tests is the Selenium framework, released by Thought­Works. We take a closer look at it in this tutorial.

What is Selenium or Selenium WebDriver?

In order to optimize the de­vel­op­ment process for a time and cost ap­pli­ca­tion in Python, Jason Huggins created the JavaScriptTestRun­ner in 2004 – the core element of the web testing framework now known as Selenium. Initially, the tool was only used in­ter­nal­ly by Thought­Works, the software company where Huggins worked at the time. After he switched to Google in 2007, he advanced the de­vel­op­ment of the open-source software (Apache 2.0 license). Following a merger with the API WebDriver, the testing framework was given its current name: Selenium or Selenium WebDriver.

Today’s version of Selenium is based ex­clu­sive­ly on HTML and JavaScript, and enables de­vel­op­ers to test and record in­ter­ac­tions with a web ap­pli­ca­tion, and repeat automated testing as often as necessary. The key com­po­nents that make this testing process possible are:

  • Selenium Core: The core module contains the basic func­tion­al­i­ty of the framework, including the JavaScriptTestRun­ner as well as the un­der­ly­ing test command API.
  • Selenium IDE: Selenium IDE is the de­vel­op­ment en­vi­ron­ment of the testing framework, which serves as a basis for the IDE extension for Chrome and Firefox, among other functions. This is required for recording and running tests.
  • Selenium WebDriver: WebDriver is the key interface for sim­u­lat­ing user in­ter­ac­tions in any browser – whether it’s Firefox, Chrome, Edge, Safari or Internet Explorer. Since 2018, the API has been an official W3C standard.
  • Selenium Grid: Selenium Grid is an extension of WebDriver or rather its pre­de­ces­sor Selenium Remote Control (RC), which enables tests to be run si­mul­ta­ne­ous­ly on multiple servers. This allows the test length to be reduced sig­nif­i­cant­ly.

Where is Selenium WebDriver used?

Selenium is an es­tab­lished name in the world of testing and is popular as a basic foun­da­tion for various programs in this context. The best-known examples include the end-to-end framework Pro­trac­tor, designed specif­i­cal­ly for testing Angular and AngularJS ap­pli­ca­tions which uses the WebDriver API to simulate user in­ter­ac­tions. The test au­toma­tion software Appium, designed for native and hybrid mobile apps, also uses the stan­dard­ized interface to quickly and con­ve­nient­ly perform tests in the chosen pro­gram­ming language.

The well-known, cloud-based web service Browser­Stack also utilizes Selenium. The service is developed in India and is available as various paid sub­scrip­tion packages following a free trial phase. It uses the testing framework as a basis for its automated desktop and mobile tests.

Note

Because they are open-source software, Selenium or Selenium WebDriver can be used in­de­pen­dent­ly of existing programs. Thanks to the various com­po­nents that have been combined in the framework, this is no problem at all – provided you have the required skills.

Selenium WebDriver tutorial: How to use the framework for your web tests

Anyone can run their own test cases with Selenium without having to depend on an external service or software man­u­fac­tur­er, and without requiring any special pro­gram­ming skills. Browser testing using the framework is actually designed to be straight­for­ward in order to write test scripts in various languages – including JavaScript, Java, C#, Ruby, and Python. In the following Selenium tutorial, we’ll demon­strate how to set up and use the framework with a Python example.

Note

The following steps of this Selenium WebDriver Tutorial require a current version of Python to be installed.

Step 1: Install Selenium

Clients as well as a range of libraries are available for using Selenium WebDriver. These are known col­lec­tive­ly as language bindings and form the basis of the framework or testing process. However, you only need to install the client drivers and libraries for the language you wish to write your script in.

In our Selenium WebDriver tutorial, we need the language bindings for Python, which are installed as standard with the Python package manager “pip” and the following command:

pip install selenium

Al­ter­na­tive­ly, download the source package via the link above, unpack the tar.gz archive, and use the setup.py file to run the in­stal­la­tion. In this case, the required command is as follows:

python setup.py install

Next, download and install the browser driver that is to be connected with Selenium. For example, Firefox requires the driver “geck­o­driv­er”; you can find its official releases in the following GitHub repos­i­to­ry. A complete list of drivers for the most common browsers (Chrome, Edge, Internet Explorer, Safari etc.) is ac­ces­si­ble in the official Selenium doc­u­men­ta­tion.

Note

If you want to use Selenium via remote con­nec­tion, you’ll also need to install the Selenium server com­po­nents. The cor­re­spond­ing in­stal­la­tion packages can be found on the official download page of the framework (in the “Selenium Server (Grid)” section). Please bear in mind that running the server ap­pli­ca­tion requires an up-to-date version of the Java runtime en­vi­ron­ment (JRE).

Step 2: Choose the right de­vel­op­ment en­vi­ron­ment (IDE)

After you’ve met the pre­con­di­tions for using Selenium WebDriver, you can start writing testing scripts for your web project. Your usual code editor is generally suitable for this purpose. But in this case, we recommend using a Python IDE (or a de­vel­op­ment en­vi­ron­ment for the language you are working with) to maximize pro­duc­tiv­i­ty. Well-known and popular solutions include the following:

  • PyCharm: The Python IDE PyCharm is available as a free, open-source community edition or as a paid pro­fes­sion­al version. However, you can write scripts for Selenium tests with both variants. Windows, Linux, and macOS are all supported operating systems.
  • PyDev: PyDev is a Python plug-in for the de­vel­op­ment en­vi­ron­ment Eclipse, which is es­sen­tial­ly intended for the de­vel­op­ment of Java ap­pli­ca­tions. The extension can either be down­loaded and installed via the project page or directly using the update manager. PyDev and Eclipse run on all common Windows, macOS, and Linux systems.

Step 3: Create Python script for browser testing (Firefox) with Selenium

Once you have your desired solution ready, you can start writing in­di­vid­ual scripts for au­tomat­ing browser in­ter­ac­tions – with the help of Selenium classes and functions. In this Selenium tutorial, we create an example Python script for Firefox, which au­to­mat­i­cal­ly opens the Google search engine in the Mozilla browser, enters a search term, and then analyzes and records the results. Seen in code form, these automated steps appear like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Start Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# Open web application
driver.get(“http://www.google.com”)
# Localize textbox
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()
# Enter and confirm search term
search_field.send_keys(“search term”)
search_field.submit()
# Open list of search results shown following the search
# using the method find_elements_by_class_name
lists= driver.find_elements_by_class_name("_Rm")
# Run through all elements and return individual text
i=0
for listitem in lists:
    print (listitem.get_attribute("innerHTML"))
    i=i+1
    if(i>10):
        break
# Close browser window
driver.quit()

The meaning of the in­di­vid­ual script com­po­nents is sum­ma­rized in the table below:

Code Meaning
from selenium import web­driver­from selenium.webdriver.common.keys import Keys First, the WebDriver module is loaded in order to implement the classes and methods for sup­port­ing different browsers. The script will then load the setup for the virtual keyboard for sim­u­lat­ing the keyboard entries at a later stage.
driver = webdriver.Firefox()driver.im­plic­it­ly_wait(30)driver.maximize_window() In the second step, a Firefox instance is created that can later be con­trolled with Selenium commands. A timeout of 30 seconds is provided for starting the browser; the browser window is also to be maximized.
driver.get(“http://www.google.com”) The script now opens the Google search page that provides a basis for the automated user in­ter­ac­tions.
search_field = driver.find_element_by_id("lst-ib")search_field.clear() Once the search engine is opened, the script will look for the Google search textbox – marked by the ID attribute “lst-ib”. As soon as it has been localized, the search field is first cleared with the method: clear().
search_field.send_keys(“search term”)search_field.submit() The text “search term” is entered and confirmed using the method: submit().
lists= driver.find_elements_by_class_name("_Rm") The in­di­vid­ual search results are sub­se­quent­ly listed as <a> elements. In order to manage them using the method find_elements_by_class_name, it is inserted in the script at this point.
i=0for listitem in lists: print (listitem.get_attribute("innerHTML")) i=i+1 if(i>10): break In the last active step, the script returns a list of search results – limited to the first ten entries with a <a> tag.
driver.quit() The final line of code then closes the browser instance.
Note

The example used in this Selenium tutorial clearly shows how well the framework is suited to browser testing. But it also demon­strates that Selenium WebDriver is an in­ter­est­ing option for web scraping with Python, which is another versatile usage pos­si­bil­i­ty offered by the testin­Google Workspace. More detailed in­for­ma­tion about this web data repos­i­to­ry can be found in our article on web scraping.

Selenium WebDriver: not suitable for all scenarios

Selenium or Selenium WebDriver offer first-rate tools for col­lect­ing important website data and sim­u­lat­ing user in­ter­ac­tions. But the open-source framework is not suitable for all areas of ap­pli­ca­tion – as the de­vel­op­ers make clear in their list of “worst cases”. These include the following cases or website content which cannot be tested or recorded with Selenium:

  • Captchas: The well-known and wide­spread captchas were developed specif­i­cal­ly to protect against bots and spam and are therefore not available for au­toma­tion processes with Selenium. The input tests should, therefore, be de­ac­ti­vat­ed during testing or scraping, or tem­porar­i­ly replaced with an al­ter­na­tive element.
  • File downloads: Although you can start down­load­ing files in Selenium instances with a simulated link click, the API does not display the progress of the download process.
  • HTTP status codes: Selenium has certain weak­ness­es when it comes to handling HTTP status codes. But these dis­ad­van­tages can be offset by using an ad­di­tion­al proxy, if necessary.
  • Login with third-party services: From social media platforms and cloud services to email accounts – signing into third-party providers via a Selenium session is not rec­om­mend­ed. On the one hand, the providers of these services offer their own APIs for test purposes. On the other, testing with the framework in these cases is very cum­ber­some.
  • Per­for­mance testing: Selenium WebDriver is not suitable for pure per­for­mance tests, since the framework is simply not designed for them.
Go to Main Menu