The is_displayed()
method in Selenium is used to check whether a web element is visible to the user on the webpage. This method returns True
if the element is visible and False
if it is hidden (e.g., using CSS display: none
, visibility: hidden
, or if it is outside the viewport).
- Selenium Features
- Selenium Installation
- Selenium Locators
- XPath Fundamentals
- CSS Selectors Methods
- Different Browsers Execution
- find_element & find_elements
- Check Enabled Status
- Check Displayed Status
- Check Selected Status
- Selenium Waits
- Send_keys Method
- Click Method
- Get Text
- Get Attribute Value
- Get Current URL
- Forward, Back, Refresh
- Take Screenshot
- Handle Browser Tabs
- Handle iframe
- Mouse Hover
- Context-Click
- Drag & Drop
- Handle Alerts
- Handle Dropdown
- Execute Javascript
- Scroll To element
- Headless Mode Execution
- Chrome Options
- Keyboard Action
Syntax:
element.is_displayed()
Example:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Set up the driver (assuming you're using Chrome)
driver = webdriver.Chrome()
# Open a webpage with website URL
driver.get('https://www.google.com')
# Locate the element (e.g., a button or div)
element = driver.find_element(By.NAME, 'q')
# Check if the element is displayed
if element.is_displayed():
print("The element is visible on the page.")
else:
print("The element is not visible on the page.")
# Close the browser
driver.quit()
Explanation:
element.is_displayed()
returnsTrue
if the element is visible to the user.- This method can be useful when you want to verify if an element is present on the page and is currently visible (not hidden or collapsed).