Introduction
A Button is one of the most commonly used form elements in web applications. It allows users to perform actions such as submitting forms, logging into applications, searching for information, and triggering various functionalities.
In Selenium, buttons are automated using methods such as click() to simulate user interactions. Buttons are frequently used in login pages, registration forms, checkout processes, and navigation workflows.
In this tutorial, you’ll learn how to locate and interact with buttons using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.
What is a Button?
A Button is an HTML element that performs an action when clicked.
Common examples include:
Login Button
Submit Button
Search Button
Register Button
Add to Cart Button
Checkout Button
Download Button
Example HTML:
<button type="submit">Login</button>
Why Automate Buttons?
Automating buttons helps you:
Submit forms.
Trigger application workflows.
Perform navigation actions.
Validate button functionality.
Verify successful transactions and page transitions.
Common Methods Used
| Method | Purpose |
|---|---|
| click() | Click the button |
| is_displayed() | Verify the button is visible |
| is_enabled() | Verify the button is enabled |
| get_attribute() | Retrieve button attributes |
| text | Retrieve the visible button text |
Example
The following example enters valid login credentials and clicks the Login button on The Internet Herokuapp login page. After successful authentication, Selenium verifies that the browser is redirected to the secure page.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 25. Form Elements - Buttons
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 25_examples/test_02_buttons.py
#
# Buttons are clicked to submit forms or trigger actions.
def test_buttons():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/login")
driver.find_element(By.ID, "username").send_keys("tomsmith")
driver.find_element(By.ID, "password").send_keys("SuperSecretPassword!")
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
assert driver.current_url.endswith("/secure")
finally:
driver.quit()
Output
The Login button is clicked successfully, and the user is redirected to the secure page after successful authentication.
Understanding the Code
Import the Required Classes
from selenium import webdriver
from selenium.webdriver.common.by import By
Imports:
webdriverfor launching and controlling the browser.Byfor locating web elements using Selenium’s locator strategies.
Create the WebDriver
driver = webdriver.Chrome()
Launches a new Chrome browser session.
Open the Practice Website
driver.get("https://the-internet.herokuapp.com/login")
Opens The Internet Herokuapp login page containing the Username field, Password field, and Login button.
Enter the Login Credentials
driver.find_element(
By.ID,
"username"
).send_keys("tomsmith")
driver.find_element(
By.ID,
"password"
).send_keys("SuperSecretPassword!")
Enters valid credentials into the Username and Password text boxes before clicking the Login button.
Locate and Click the Button
driver.find_element(
By.CSS_SELECTOR,
"button[type='submit']"
).click()
Locates the Login button using its type attribute and clicks it using Selenium’s click() method.
The click() method simulates an actual mouse click performed by the user.
Buttons are commonly located using:
ID
Name
CSS Selector
XPath
Verify Successful Login
assert driver.current_url.endswith("/secure")
Verifies that the browser was redirected successfully after clicking the Login button.
If the assertion passes successfully, it confirms that:
The button was clicked successfully.
The login operation completed successfully.
Selenium navigated to the secure page.
Close the Browser
driver.quit()
Closes the browser and ends the WebDriver session.
This is a recommended practice to ensure that all browser instances are properly terminated after test execution.
Verifying Whether a Button is Visible
Before clicking a button, you can verify whether it is visible on the webpage.
Example:
submit_button.is_displayed()
This helps prevent failures caused by hidden elements.
Verifying Whether a Button is Enabled
Sometimes buttons remain disabled until certain conditions are satisfied.
Example:
submit_button.is_enabled()
This method verifies whether the button is ready for interaction.
Reading the Button Text
You can retrieve the visible text displayed on the button.
Example:
submit_button.text
Output:
Login
This is useful when validating user interface elements during automation testing.
Practical Example
Suppose an e-commerce website contains a Search button.
The automation script:
Enters a product name.
Clicks the Search button.
Verifies that the search results page loads successfully.
This validates both the button functionality and application workflow.
Automation Testing Example
Consider an online banking application.
The login page contains:
Username textbox
Password textbox
Login button
The automation script:
Enters the username.
Enters the password.
Clicks the Login button.
Verifies that the user dashboard loads successfully.
Buttons are often the entry point for critical business workflows in automation testing.
Real-World Example
Buttons are commonly used in:
Banking applications
E-commerce websites
CRM systems
Healthcare portals
HR management systems
Government websites
Enterprise web applications
Almost every modern web application contains multiple buttons that perform important business operations.
Advantages of Automating Buttons
Simulates real user interactions.
Validates business workflows.
Improves automation coverage.
Supports end-to-end testing.
Reduces manual testing effort.
Common Mistakes Beginners Make
Clicking Disabled Buttons
Always verify whether the button is enabled before clicking it.
Example:
button.is_enabled()
Using Incorrect Locators
Always prefer stable locators such as:
ID
Name
CSS Selector
Avoid brittle XPath expressions whenever possible.
Ignoring Synchronization
Buttons may load dynamically after API calls or animations.
Use Explicit Wait whenever necessary before clicking buttons.
Clicking Invisible Elements
Always verify that the button is visible before interacting with it.
Example:
button.is_displayed()
Best Practices
Prefer locating buttons using ID or CSS Selector.
Verify that buttons are visible and enabled before clicking them.
Use Explicit Wait for dynamically loaded buttons.
Validate navigation or application behavior after button clicks.
Use reliable and maintainable locators.
Conclusion
Buttons are among the most frequently automated web elements in Selenium because they initiate important user actions across web applications. By using methods such as click(), is_displayed(), and is_enabled(), you can reliably automate button interactions and validate application workflows. Following best practices such as using stable locators and proper synchronization helps create robust and maintainable automation scripts.
Frequently Asked Questions (FAQs)
Which method is used to click a button?
Use:
click()
How can I verify whether a button is visible?
Use:
is_displayed()
How can I verify whether a button is enabled?
Use:
is_enabled()
Which locator is best for locating buttons?
The preferred locators are:
ID
Name
CSS Selector
Are buttons commonly automated in Selenium?
Yes.
Buttons are among the most frequently automated web elements because they perform important actions such as logging in, submitting forms, searching data, and completing transactions.
Key Takeaways
Buttons perform important actions in web applications.
Use
click()to interact with buttons.Use
is_displayed()andis_enabled()for validation.Prefer stable locators such as ID and CSS Selector.
Use Explicit Wait for dynamically loaded buttons.
Validate the application’s behavior after button clicks.
Proper synchronization and locator selection improve automation reliability.
