In this Python Selenium tutorial we want to learn about Verify Displayed Web Elements in Python Selenium, there are three methods in selenium web driver that we can check the visibility of the web elements in Python Selenium, we have is_displayed(), is_enabled() and is_selected(). so some times we need to verify the displayed element in a web page, when we are going to automate our web pages, some times we need to find that some web elements are displayed on a web page or some web elements are enabled in a web page.
is_displayed() method
is_displayed action verifies if an element is displayed on the web page and can be executed on all web elements. the is_displayed() method returns a boolean value specifying whether the target element is displayed or not displayed on the web page.
following is the code to verify if the Google Input field is displayed or not, which obviously should return true in this case.
1 2 3 4 5 6 7 8 9 10 11 |
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://www.google.com') search_box = driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div' '[1]/div[1]/div/div[2]/input') print(search_box.is_displayed()) |
In the above example we have found the targeted web element using XPath, and after that we have used the is_displayed() method from selenium web driver to check the element.
Run the code and we are receiving true in the console, because the web element is displayed in the page.
is_enabled method()
The is enabled action verifies if an element is enabled on the web page and can be executed on all web elements. the is_enabled() method returns a Boolean value specifying whether the target element is enabled or not displayed on the web page.
The following is the code to verify if the Google Input Field is enabled or not, which obviously should return true in this case.
1 2 3 4 5 6 7 8 9 10 11 |
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://www.google.com') search_box = driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div' '[1]/div[1]/div/div[2]/input') print("Is Enabled Is : ", search_box.is_enabled()) |
The preceding code uses the is_enabled() method to determine if the element is displayed on a web page. The preceding code returns true for the Google Input Field.
Run the code and you will receive true.
is_selected() method
The is_selected() action verifies if an element is selected right now on the web page and can be executed only on a radio button, options in select, and checkbox web elements. When executed on other elements, it will return false.
The preceding method returns a Boolean value specifying whether the target element is selected or not selected on the web page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://the-internet.herokuapp.com/checkboxes') check_box = driver.find_element_by_xpath('//*[@id="checkboxes"]/input[1]') check_box2 = driver.find_element_by_xpath('//*[@id="checkboxes"]/input[2]') print("Check Box One : ", check_box.is_selected()) print("Check Box Two : ", check_box2.is_selected()) |
The preceding code uses the is_selected() method. and we have two checkboxes, one is selected and the second is not selected.
Run the code and this is the result