UI Automation using Python and Selenium: Tutorial

On This Page What is UI Automation Testing?Components of a UIMarch 05, 2026 · 7 min read · Tool Comparison

UI Automation using Python and Selenium: Tutorial

Selenium is a widely used tool for automating web applications, and Python do it easy to write and negociate trial scripts. It allows testers to simulate user actions like clicking buttons, fill out forms, and navigating pages. With support for multiple browsers and integration with frameworks like PyTest, Selenium enhances test execution and reportage.

This tutorial covers everything related to UI mechanisation use Python, including fundamentals, key UI ingredient, and how to use Selenium with Python for effective test automation.

What is UI Automation Testing?

UI automation testing verifies that an application & # 8217; s user interface behaves as expected by simulating existent user interactions through scripts and tools. It ensures that elements like buttons, kind, and navigation carte function correctly across different devices and browsers.

using Python helps observe ocular and functional issues betimes, improving software reliability. Selenium, when used with Python, enable automated testing by interacting with web elements, executing test event, and formalise UI behavior efficiently.

Read More:

Components of a UI

User interface consist of respective components, each serving a specific function establish on the application & # 8217; s needs. These components include input control, pilotage components, container, and informational constituent, all working together to create a seamless user experience.
Depending on the use there are a few major part that exist in every UI:

  • Input control, these control capsule all vista of a UI involved with user input.

As seen above ingredient such as the button “Add to drag”, dropdown lists, checkboxes, toggle, or text fields all fall under input controls.

Read More:

  • Navigation Components, are navigational controls used by the user to navigate the application.

Menus, buttons to sail to different pages, lookup bars, and breadcrumbs are all classic navigational components.

  • Containers, this component holds content together.

For example, pictured above is a popular container called an Accordion container.

  • Informational Components, these components provide the user with information regarding the application, detail about products, or advertisements.

For Example:Message boxes, notifications, icons, and tooltips.

Why choose Selenium for UI mechanisation testing

is a powerful mechanisation model that is extremely realize and established in its use for testing web applications.

Distinguishing Features of Selenium:

  1. It supports multiple programming lyric such as Python, Java, JavaScript, Ruby, C #, Perl, and PHP.
  2. Selenium also back a turgid variety of popular browsers like Google Chrome, Mozilla Firefox, Internet Explorer, Safari, and many others; In addition to this Selenium support various well-established OS ’ such as Linux, Mac, Windows, Android, and iOS.
  3. is a very popular feature of Selenium, allowing the user to perform and giving them the ability to configure and control the browsers on the OS level. The WebDriver is capable of communicating directly with the browser without the need for any average waiter, thus allowing it to action trial cases quicker than most other.
  4. Fewer resources are required to work with Selenium in contrast with other automation frameworks.
  5. Selenium is open-source.
  6. Selenium makes it easy to automatize web applications by utilize. Locators let the developer to identify ingredient on a web page by certain attributes like ID,,, name, class name, tag name, link text, and partial link text.

Also Read:

UI Testing with Selenium and Python: Example

UI automation using Python and Selenium is performed in this example. This explores the user interface of the site “https: //www.bstackdemo.com/” and transmit out an end-to-end user process. This summons involve activeness a typical user might do on the web application such as:

Logging in & gt; Navigating to a certain headphone brand & gt; Liking product & gt; Adding a product to the cart & gt; Checking out & gt; Entering customer details and delivery address & gt; Concluding the purchase & gt; Downloading the receipt.

Pre-Requisites:

  • Set up a Python environment.
  • Install Selenium. If you have conda or anaconda set up then habituate the pip package installer would be the leisurely method to do so. Simply run this command (on anaconda prompting, or directly on the Linux terminal):
  • Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

pip install selenium
  • Download the up-to-the-minute WebDriver for the browser you wish to use; Chrome Webdriver was used for this model, or establish the webdriver_manager package by running the command:
pip install webdriver_manager

Step 1: Import the needed software.

from selenium signification webdriver

from selenium.webdriver.chrome.service meaning Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.keys import Keys

Step 2: Navigate to the example web application.

driver = webdriver.Chrome (service=Service (ChromeDriverManager () .install ())) driver.maximize_window () wait = WebDriverWait (driver, 60) driver.get ('https: //www.bstackdemo.com/ ') wait.until (EC.url_to_be ('https: //www.bstackdemo.com/ '))

First, the driver for google chrome is installed or updated as needed. Following this, the chrome browser window is maximized to fill the screen. Finally, we navigate to “https: //www.bstackdemo.com/”, the website whose UI will be tested in this example.

To ensure that the website loads properly explicit wait is employed, this makes the program wait until the url is “https: //www.bstackdemo.com/”.

Also Read:

Step 3: Automate User Login.

sign_in=driver.find_element (By.ID, `` signin '') sign_in.click () WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, `` /html/body/div/div [2] /div/form/div [2] /div [1] /div/div [1] /div [1] ''))) .click () WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, `` /html/body/div/div [2] /div/form/div [2] /div [1] /div/div [1] /div [1] /div [1] ''))) .click () active_ele = driver.switch_to.active_element active_ele.send_keys (`` demouser '') active_ele.send_keys (Keys.ENTER) WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, `` /html/body/div/div [2] /div/form/div [2] /div [2] /div/div [1] ''))) .click () WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, `` /html/body/div/div [2] /div/form/div [2] /div [2] /div/div [1] /div [1] ''))) .click () active_ele = driver.switch_to.active_element active_ele.send_keys (`` testingisfun99 '') active_ele.send_keys (Keys.ENTER) sign_in=driver.find_element (By.ID, `` login-btn '') sign_in.click ()

The ID locater is utilise to find and tick on the sign-in button element. The is so used to find the textbox for inputting the username. The driver is switched to the active element (the textbox for username), and the key “ demouser ” are entered into this textbox.

Read More:

The XPath locator is use once again to encounter the textbox for entering the password. The driver is switched to the fighting component (the textbox for the password), and the keys “testingisfun99” are entered into this textbox.

Once again explicit wait is employed with an expected stipulation to make the program hold until the text fields of the username and word are clickable.

Lastly, the ID locator is used to find and tick the login button element.

Step 4: Add a Google phone to the handcart and like another phone.

# Select a Google phone and add it to the pushcart WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, `` /html/body/div/div/div/main/div [1] /div [3] /label/span ''))) .click () # Like a phone WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, `` /html/body/div/div/div/main/div [2] /div [2] /div [1] /button ''))) .click () WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, '' /html/body/div/div/div/main/div [2] /div [3] /div [4] ''))) .click () WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.CLASS_NAME, '' buy-btn ''))) .click ()

The XPath factor is habituate to find and click on the “ Google & # 8221; vendor element in order to filter for google phones. The Pixel 4 ’ s like button is found using the and snap; To simulate how a exploiter may like sure products and buy others. Lastly, the XPath locator is used to add the Pixel 3 phone to the cart, and snap the checkout button.

Once again explicit wait is employed with an expected condition to make the program wait until the Google vendor element, like button, add to cart button, and checkout button is clickable.

Step 5: Fill in the necessary details and Checkout.

# Checkout WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.ID, `` firstNameInput ''))) .click () active_ele = driver.switch_to.active_element active_ele.send_keys (`` Alice '') active_ele.send_keys (Keys.ENTER) WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.ID, `` lastNameInput ''))) .click () active_ele = driver.switch_to.active_element active_ele.send_keys (`` Cooper '') active_ele.send_keys (Keys.ENTER) WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.ID, `` addressLine1Input ''))) .click () active_ele = driver.switch_to.active_element active_ele.send_keys (`` Apt.5, Downton Building, Cherryblossom Road '') active_ele.send_keys (Keys.ENTER) WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.ID, `` provinceInput ''))) .click () active_ele = driver.switch_to.active_element active_ele.send_keys (`` Canterbury '') active_ele.send_keys (Keys.ENTER) WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.ID, `` postCodeInput ''))) .click () active_ele = driver.switch_to.active_element active_ele.send_keys (`` CT3 '') active_ele.send_keys (Keys.ENTER)

At checkout a variety is provided to the user, request info such as their name and address. This programme uses the ID locater to find and click the textbox elements for this information; Explicit wait is used to wait until the textbook boxes are clickable. Once the requisite element has been launch the driver switches to this active ingredient and sends the keys pertinent to it. For example, upon finding the textbox with the ID “lastNameInput” the keys “Cooper” are sent to it as input.

Step 6: Lastly Download the Receipt.

WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.ID, '' downloadpdf ''))) .click () driver.quit ()

Upon enrol all the mandatory information into the form to checkout, the user is led to a page with their receipt for the product. Once again explicit hold is engage with an expected condition to get the program wait until the link for the reception is clickable.

The ID locater is utilize to find the link to download the receipt and click on it, hence conclude one likely interaction a user may have with the web application.

Output:

Confirmation:

Talk to an Expert

Conclusion

Selenium and Python provide an efficient and flexible solution for automating UI testing. Combining them allows teams to enhance test reportage and performance. It besides volunteer an easy way to get started, even for those with circumscribed automation experience, and delivers robust test results that can be applied across different browser and environs.

While Selenium can simulate interactions, it may not full retroflex the performance or specific behaviors of existent devices. offers entree to 3500+ real devices so you can acquit exam that check not just the look but also the responsiveness, touch interaction, and functionality of your app across a wide wide range of.

Useful Resources for Selenium and Python

Tags
79,000+ Views

# Ask-and-Contributeabout this subject with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free