Web applications receive new updates and features every few weeks for higher user engagement. Automation testing is necessary to test these features and ensure the UI works well. For Python testers worldwide, Selenium is the first choice for executing automated tests.
Python is one of the most popular and on-demand programming languages right now. The reason is obvious; it offers plenty of benefits to its users and is considered an all-rounder. You can create websites using Python by leveraging tools such as Flask, Django, and so on. It is also possible to perform web automation using Selenium.
Also, you can use Python for Data Science and Machine Learning, etc. Its simple syntax and the tons of libraries and packages it provides, make it a favorite among programmers.
Overview of Selenium
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc.
Selenium Tutorial covers all topics such as – WebDriver, WebElement, Unit Testing with Selenium.
Why learn Selenium Python?
- Open Source and Portable. Selenium is an open-source and portable Web testing Framework.
- Combination of tool and DSL. Selenium is a combination of tools and DSL (Domain Specific Language) in order to carry out various types of tests.
- Easier to understand and implement. Selenium commands are categorized in terms of different classes which make it easier to understand and implement.
- Less burden and stress for testers. As mentioned above, the amount of time required to do testing repeated test scenarios on each and every new build is reduced to zero, almost. Hence, the tester’s burden is reduced.
- Cost reduction for the Business Clients. The Business needs to pay the testers their salary, which is saved using an automation testing tool. Automation not only saves time but also provides cost benefits too, to the business.
Blending Selenium with Python
The first step for you to start working on Selenium with Python is that you need to write functional test cases using the Selenium web driver. Then, you need to forward a request to the Selenium server that sits at the back end, which will execute the test cases on the browsers automatically.
You can perform tests on any browser including Firefox, Chrome, IE, etc. The only thing that changes are the web drivers, which are specific for each browser.
Blending Selenium with Python usually works via APIs – which you can use to write functional or acceptance test cases with the help of the Selenium web driver. Using this API, you can easily access all the different types of functionalities in an intuitive manner.
Installing the package for using Selenium with Python
The first step is to install the Selenium package for Python. You can do so using the simple pip command.
$ pip install selenium
While the installation of Selenium makes the functionality available to you, you need additional drivers for it to be able to interface with a chosen web browser. The download links for the drivers are listed below:
For the remainder of this Python Selenium tutorial, we will use Chromedriver. Follow the link for the browser of your choice and download the driver for the compatible version.
If you only plan to test Selenium locally, downloading the package and drivers should suffice. However, if you would like to set Selenium up on a remote server, you would additionally need to install the Selenium Server. Selenium Server is written in Java; you need JRE 1.6 or above to install it on your server. It is available on Selenium’s download page.
How to run your first test on Selenium with Python
Once you have completed the pre-requisites section, you can start your first test in Selenium with the Python programming language!
1. First, import the WebDriver and Keys classes from Selenium.
from selenium import webdriver from selenium.
webdriver.common.keys import Keys
The WebDriver class will connect you to a browser instance. The Keys class lets you emulate the stroke of keyboard keys, including special keys like “Shift” and “Return”.
2. Next, create an instance of Chrome with the path of the driver you downloaded through the websites of the respective browser. This example assumes the driver is in the same directory as the Python script you will execute.
driver = webdriver.Chrome('./chromedriver')
If you are testing on your local machine, this opens an instance of Chrome locally. This command lets you perform tests on it until you use the .close() method to end the connection to the browser.
3. Next, use the .get() method of the driver to load a website. You may also load a local development site as this process is equivalent to opening a window of Chrome on your local machine, typing a URL, and hitting Enter. The .get() method starts loading a website and waits for it to render completely before moving on to the next step.
driver.get("https://www.python.org")
4. Once the page loads successfully, you can use the .title attribute to access the textual title of the webpage. If you wish to check whether the title contains a particular substring, use the assert or if statements. For simplicity, let us print the title of the page.
print(driver.title)
The output is the following text:
Welcome to Python.org
If you run the test on a Python interpreter, you notice that the Chrome browser window is still active. Also, a message on Chrome states that automated software is currently controlling it.
5. Next, let us submit a query in the search bar. First, select the element from the HTML DOM, enter a value into it, and submit the form by emulating the Return key press. You can select the element using its CSS class, ID, name attribute, or tag name. If you check the source of the query search bar, you notice that the name attribute of this DOM element is “q”.
Therefore, you can use the .find_element_by_name() method as follows to select the element.
search_bar = driver.find_element_by_name("q")
6. Once the DOM element is selected, you first need to clear its contents using the .clear() method, enter a string as its value using the .send_keys() method and finally, emulate the press of the Return key using Keys.RETURN.
search_bar.clear()
search_bar.send_keys("getting started with python") search_bar.send_keys(Keys.RETURN)
You notice in the window that these actions trigger a change in the URL with the search results in the window. To confirm the current URL of the window, you can use the following command.
print(driver.current_url)
The following string is displayed:
'https://www.python.org/search/?q=getting+started+with+python&submit='
To close the current session, use the .close() method. It also disconnects the link with the browser.
driver.close()
In this example, we have looked at the steps involved in running our first test using Selenium Python. Do note that we kept the window open during all test stages to ensure you knew what happened in the background as you ran each command.
In a fully automated flow, you will run multiple tests sequentially and may not be able to view each step as they occur.
To summarise the discussion, here is your first Selenium test on Python. You may save it in the file selenium_test.py and run python selenium_test.py to run the test.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver') driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python") search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
Selenium WebDriver Methods
The parent of all the classes and methods that are used for Selenium with Python is the Selenium WebDriver. Using this, you can perform any type of function or operation on any element on a webpage, hence, it is considered as the driving force of Selenium. It has a ton of attributes and methods that are used to aid automation testing in Selenium Python.
Some of the most frequently used methods for Selenium WebDriver are:
Method | Description |
add_cookie | This method is used to add cookies to the user’s current session. |
back | This method allows you to move one page back. |
close | This method will close the current window. |
create_web_element | This method will create a web element having a specific element_id. |
delete_all_cookies | It will help you delete all the cookies in the current session’s scope. |
delete_cookie | It will only delete a single cookie based on the name. |
execute_async_script | It will execute JavaScript asynchronously in the current frame. |
execute_script | It will execute JavaScript synchronously in the current frame. |
forward | It will help you go one page forward. |
fullscreen_window | This method will invoke a ‘full screen’ operation specific to the window manager. |
get_cookie | It will return a cookie if found, or else none. |
get_cookies | It will return a bunch of dictionaries, each corresponding to the cookies that are currently visible in the present session. |
get_log | For a log type, you will get all the logs. |
get_screenshot_as_base64 | You can get a screenshot of the present window as a base64 encoded string. |
get_screenshot_as_file | You will get the screenshot of the present window as a binary file. |
get_screenshot_as_png | It will save a screenshot of the present window as a PNG file. |
get_window_position | You will get the coordinates of the present window. |
get_window_rect | You will get the coordinates of the present window along with the width and height. |
get_window_size | You will get only the height and width of the present window. |
implicitly_wait | You will be able to set a timeout to wait implicitly for an event to occur. |
maximize_window | You can maximize the present window that is being used by the web driver. |
minimize_window | You will be able to minimize the present window. |
quit | You will be able to quit the driver, closing all the associated windows. |
refresh | You will be able to refresh the current frame. |
set_page_load_timeout | It will allow you to set a time to wait before page loads or else it will throw an error. |
set_script_timeout | You will be able to set the time that the script needs to wait during the call of an execute_async_script before yielding an error. |
set_window_position | You will be able to set the coordinates of the current window. |
set_window_rect | You will be able to set the coordinates of the current window along with the height and width. |
current_url | You can get the URL of the web page. |
current_window_handle | You can get the handle of the window. |
page_source | You will get the current page’s source. |
title | It will return the current page’s title. |
WebElement methods for Selenium With Python
The class called selenium.webdriver.remote.webelement.WebElement encloses a plethora of elements within it. These elements can be property, class, tag, or anything. For example, when you find an element in a web page using the driver, you can perform certain actions such as clicking on it or finding the sub-elements. For this, you can leverage several Selenium WebElement methods.
Some of the popular Selenium WebElement methods are:
Element Methods | Description |
is_selected() | This method returns a boolean value based on whether the element is selected or not. |
is_displayed() | This method returns a boolean value based on whether or not the element is displayed. |
is_enabled() | This method returns a boolean value based on whether or not the element is enabled. |
get_property() | This method can be used to get the properties of a particular element, like text_length property, etc. |
get_attribute() | This method can be used to get attributes of a particular element, like getting an href attribute, etc. |
send_keys() | This method is used to send a text property to any input field or even an anchor tag or a paragraph. |
click() | This method is used to click on any element. |
clear() | This method is used to clear the text of any input field. |
screenshot_as_png() | This method is used to take a screenshot of the current element. |
submit() | This method is used to submit a form. |
value_of_css_property() | This method is used to get the value of a specific element’s CSS property. |
location | This method is used to get the location of the elements. |
screenshot() | This method is used to take the screenshot in the form of binary data. |
parent | This method is used to get the parent of the current element. |
size | This method is used to receive the size of the current element. |
tag_name | This method is used to get the tag name. |
text | This method is used to get the text. |
rect | This method is used to get a dictionary containing the location and size of the element. |
screenshot_as_base64 | This method is used to take the screenshot as a base64 encoded string. |
How to integrate Selenium with Python Unit Tests?
Let us understand how to integrate Selenium tests into Python unit tests. For this purpose, we will use the unit test module in Python.
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class ChromeSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome('./chromedriver')
def test_search_in_python_org(self):
driver = self.driver
driver.get("https://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("getting started with python")
elem.send_keys(Keys.RETURN)
assert "https://www.python.org/search/?q=getting+started+with+python&submit=" == driver.current_url
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
In this example, you must set up the driver object when initializing the unit test class .Chrome() method. In the single test we demonstrate, the exact text is put on the search bar, and the resultant change in the URL is compared to the URL seen earlier.
Additionally, you may write a different test for a different browser and reuse the same functionality.
Recommended Articles
- How to Become a Cloud Architect: Step-By-Step Guide
- Computer Degrees: 11+ Highest Paying Online Tech Degree for 2024
- Google Cloud Platform: Everything You Need to Know
- PII In Cybersecurity: What Is It And How Does It Work?
- Cybersecurity Risk Assessment: What It Is & How To Perform It
- CIA Triad in Cybersecurity: What Is It & Why Is It Important?