What is Selenium WebDriver?
In general, Selenium is a set of tools for web browser automation. The tool we’re interested in is Selenium WebDriver, which is an API that makes calls to the browser in order to emulate user actions. It’s done by manipulating DOM elements in the website’s code, for example sending a text to input fields or clicking buttons. WebDriver is a platform and language neutral, which complies with the W3C WebDriver specification. There are bindings in many languages, including Python.
To use Selenium WebDriver in Python, we first have to install it:
pip install selenium
Out of the box, there is Firefox driver and Chrome driver available in the package. To show you the examples, I will use a driver for Chrome.
The very basic way of using webdriver is as follows:
from selenium import webdriver driver = webdriver.Chrome()
After you initialize the driver, you can use its instance by writing browser.perform_an_action(). You can also name it as you wish, not necessarily “driver”, but remember to stick to one convention and to keep the name clear.
Navigating on the page with webdriver is quite simple:
Remember to always state the protocol (e.g. http) when you pass the url to the method, as the driver might not recognize url as valid without that. There are also two other methods for navigating and they allow you to move forward or backward -
driver.back(), however, their behavior is very dependent on the driver you are using.
Now, quitting the browser can be a bit tricky. There are two methods that are commonly used and while they seem very similar, there is a difference in how they work:
- driver.close() - closes browser window that driver currently has focus on
- driver.quit() - closes all browser windows and ends the session
Locating page elements
There are many ways of locating HTML elements, and depending on how they are coded, some might be more useful than the others. WebDriver offers methods to find elements by:
- Name - find_element_by_name()
- ID - find_element_by_id()
- XPath - find_element_by_xpath()
- Link text - find_element_by_link_text()
- Partial link text - find_element_by_partial_link_text()
- Tag name - find_element_by_tag_name()
- Class name - find_element_by_class_name()
- CSS Selector - find_element_by_css_selector()
So locating an element that looks like this in HTML:
<input type=“text” name=“some_name” id=“some_id” />would look like this:
You can also locate multiple elements by adding “s” after element - e.g.
find_elments_by_name(“some_name”) - it will return a list of elements with name attribute called “some_name”. However, it doesn’t work with ID’s since every element needs a unique ID.
More readable, in my opinion, way to locate elements is by using private methods
find_elements. You use them by passing the way of locating the element and the locator (note that in this way they are in uppercase), for example, if you wanted to locate element by class name, it would look like this:
from selenium.webdriver.common.by import By driver.find_element(By.CLASS_NAME, “class name”)
In this case, you can more easily adjust your code if locators change or when you want to change the way of locating page elements.
WebDriver is very useful in filling forms, that otherwise done manually could be really time-consuming, especially in tests that are performed repetitively. Most common elements in forms are input fields, dropdowns, checkboxes, and buttons. To perform any actions on them, we first have to find the element using the methods presented above. To make the code more readable and clean, it’s better to find the element once and give it a name to use it as an object. Some of the most commonly used methods for interacting with page elements are:
- clear() - it clears the input if it has any text inside
- send_keys() - sends a text or a key to the input, in the first case usage looks like this element.send_keys(“text”) and for the second option element.send_keys(Keys.ENTER)
- click() - it simply clicks the element if it is in a clickable state
Example of a simple test
Now that you have some basic WebDriver knowledge, I can show you a simple test using methods I’ve shown above.
from selenium import webdriver from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome() driver.get("http://www.python.org") assert "Python" in driver.title element = driver.find_element_by_id(“id-search-field") element.clear() element.send_keys("django") element.send_keys(Keys.ENTER) assert “django" in driver.page_source driver.quit()
Firstly, Chrome driver is initialized and it’s used to navigate to python.org page. Then, in the assertion, test checks if the title of the page is correct. If yes, then the test looks for an element with id “id-search-field” that is a search input. I assumed that there could be some text already typed in, so just in case there is element.clear() to remove it. Next, the text is sent to the input and right after that ENTER key, so that the page perform a search for “django”. And lastly, we check if word “django” actually exists in the page source code, so if the search was successful, and we quit the browser.
Beyond software testing
Selenium WebDriver is a very useful and powerful tool for test automation, that can be used with most of the popular programming languages - no matter if you prefer Java, C# or Python. What is more, due to its features, it can be used in fields not necessarily connected with software testing. An example of such non-standard use in Python can be data science and web scraping. With Pandas (Python data analysis toolkit) and libraries like Beautiful Soup (that help you parse HTML code of the page and get useful information from it), WebDriver can help you with data scraping - for example by retrieving links. Another use that can be interesting as a form of programming training is writing your own bot that will perform specific actions on websites for you. Just make sure it’s not against the website policy.
Hope you’ll find this basic Selenium WebDriver tutorial useful and take advantage of test automation in your future work. For more information on it, you can check the official documentation. If you are interested in upcoming features, be sure to check out Selenium 4.0 when it comes out.