How to find the status of an element in a page in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can find the status of an element in a page with the help of Selenium. We can get the information if an element is enabled or disabled. Also, we can verify if an element is visible on screen for the user interaction or not.

On a web page, there may be numerous checkboxes or radio buttons. Selenium provides a method to check if these UI elements are in a selected state or not.

There are multiple methods to verify the status of an element. They are listed below −

  • is_selected()

    This method verifies whether an element (checkbox, radio button) is in selected condition or not. A Boolean value of either TRUE or FALSE is returned.

Syntax

driver.find_element_by_class_name("prom").is_selected()
  • is_dispayed()

    This method verifies whether an element is visible for users or not. A Boolean value of either TRUE or FALSE is returned.

Syntax

driver.find_element_by_class_name("prom").is_displayed()
  • is_enabled()

    This method verifies whether an element is in enabled status or not. A Boolean value of either TRUE or FALSE is returned.

Syntax

driver.find_element_by_class_name("prom-user").is_enabled()

Example

Code Implementation with the above methods.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm")
#to refresh the browser
driver.refresh()
# identifying the checkbox with xpath
chk =driver.find_element_by_xpath("//*[@value='Automation Tester']")
# printing the status in console
print(chk.is_selected())
# identifying the edit box with xpath
edt =driver.find_element_by_xpath("//*[@name='firstname']")
# printing the display status in console
print(edt.is_displayed())
# identifying the edit box with xpath
edtsts =driver.find_element_by_xpath("//*[@name='lastname']")
# printing the enabled status in console
print(edtsts.is_enabled())
#to close the browser
driver.close()
raja
Published on 29-Jul-2020 13:04:11
Advertisements