Introduction
When performing automation testing, it’s important to check if a specific element is visible before interacting with it. This ensures that your test scripts don’t fail unexpectedly due to missing or hidden elements.
In this tutorial, you’ll learn how to check element visibility in Selenium Python using a real test case from:
👉 Test Page
We will cover:
✅ How to check if a button, input field, or link is visible
✅ Using explicit waits for elements that load dynamically
✅ Best practices to ensure reliable and stable test automation
Why Checking Element Visibility Is Important?
Ensuring an element is visible before interacting with it is essential because:
🔹 Prevents Test Failures – Avoids clicking or typing into non-existent elements.
🔹 Handles Dynamic Elements – Many web pages load elements with delays.
🔹 Improves Test Accuracy – Ensures elements are visible before interaction.
🔹 Ensures UI Functionality – Verifies that key elements are displayed correctly.
Now, let’s set up Selenium Python and learn how to check element visibility!
Setting Up Selenium Python for Testing
Step 1: Install Selenium
First, ensure you have Selenium installed by running:
pip install selenium
Step 2: Import Required Libraries
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Step 3: Open a Test Page
driver = webdriver.Chrome(executable_path="path_to_chromedriver")
driver.get("https://training.qaonlinetraining.com/testPage.php")
How to Check If an Element Is Visible in Selenium Python
1️⃣ Checking Button Visibility Using is_displayed()
The easiest way to check if an element is visible is by using the is_displayed()
method:
button = driver.find_element(By.ID, "testButton")
if button.is_displayed():
print("Button is visible on the page.")
else:
print("Button is NOT visible.")
✅ If the button is visible, the test passes.
❌ If the button is hidden, Selenium returns False
.
2️⃣ Handling Dynamic Elements Using Explicit Waits
Some elements may take time to appear on a webpage. To handle this, we use explicit waits:
wait = WebDriverWait(driver, 10)
button = wait.until(EC.visibility_of_element_located((By.ID, "testButton")))
if button:
print("Button is visible on the page.")
🔹 This method waits for up to 10 seconds for the button to become visible before checking.
3️⃣ Checking Visibility of Input Fields and Links
Checking Input Field Visibility
input_field = driver.find_element(By.NAME, "testInput")
if input_field.is_displayed():
print("Input field is visible.")
else:
print("Input field is NOT visible.")
Checking If a Link Is Visible
link = driver.find_element(By.LINK_TEXT, "Test Link")
if link.is_displayed():
print("Link is visible on the page.")
else:
print("Link is NOT visible.")
Handling Hidden Elements in Selenium Python
Some elements exist in the HTML but are hidden using CSS styles like display: none
or visibility: hidden
. These elements cannot be clicked or interacted with even if they are in the page source.
Example: Checking for Hidden Elements
hidden_element = driver.find_element(By.ID, "hiddenDiv")
if hidden_element.is_displayed():
print("Element is visible.")
else:
print("Element is hidden.")
🔹 If an element is hidden, Selenium returns False
.
🔹 If an element is visible, Selenium allows interaction.
Complete Code with result:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Initialize WebDriver
driver = webdriver.Chrome(executable_path="path_to_chromedriver")
# Open the test page
driver.get("https://training.qaonlinetraining.com/testPage.php")
# Function to check if an element is visible
def is_element_visible(locator_type, locator_value):
try:
element = driver.find_element(locator_type, locator_value)
return element.is_displayed()
except:
return False
# 1️⃣ Check if Button is Visible
button_visible = is_element_visible(By.ID, "testButton")
print("Button is visible." if button_visible else "Button is NOT visible.")
# 2️⃣ Check if Input Field is Visible
input_visible = is_element_visible(By.NAME, "testInput")
print("Input field is visible." if input_visible else "Input field is NOT visible.")
# 3️⃣ Check if Link is Visible
link_visible = is_element_visible(By.LINK_TEXT, "Test Link")
print("Link is visible." if link_visible else "Link is NOT visible.")
# 4️⃣ Check if an Element is Hidden
hidden_element_visible = is_element_visible(By.ID, "hiddenDiv")
print("Hidden element is visible." if hidden_element_visible else "Hidden element is NOT visible.")
# 5️⃣ Handling Dynamic Elements Using Explicit Waits
try:
wait = WebDriverWait(driver, 10)
dynamic_button = wait.until(EC.visibility_of_element_located((By.ID, "dynamicButton")))
print("Dynamic button is visible.")
except:
print("Dynamic button is NOT visible within the time limit.")
# Close the browser
driver.quit()
Execution Result
How This Works:
✅ Checks multiple elements (button, input field, link, hidden element, and dynamic button).
✅ Uses is_displayed()
to determine visibility.
✅ Handles dynamic elements with WebDriverWait
.
✅ Uses try-except
to avoid failures if an element is missing.
Best Practices for Checking Element Visibility
✅ Use Explicit Waits – Prevents errors due to slow-loading elements.
✅ Combine Methods – Use is_displayed()
with visibility_of_element_located()
.
✅ Handle Exceptions – Use try-except
to avoid failures if an element is missing.
✅ Check Element State – Some elements may be visible but disabled (not clickable).
Final Thoughts
Checking if a button, input field, or link is visible is an important part of automation testing. By using Selenium Python, testers can ensure stability in test execution and reduce failures caused by missing elements.
For a more detailed tutorial on Selenium automation, visit 👉 Training QA Online
If you’re ready to master Selenium automation, enroll in:
👉 Selenium Certification Training
Need to learn Python first? Start with this beginner-friendly course:
👉 Learn Python: Beginner to Advanced
🚀 Take your career to the next level with Python & Selenium automation!