This is our fourth article in Python Selenium, in this article we are going to learn about Selenium is_displayed(), is_enabled() Method also we are going to learn about is_selected() Method. 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. there are three methods that you can use is_displayed(), is_enabled() and is_selected() Method, that we are going to talk about these methods .
Python Selenium Articles
1: Python Selenium Introduction & Installation
2: Python Selenium Web Elements Introduction
3: Python Selenium Web Driver Commands
is_displayed() method
The 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.
The following is the code to verify if the Google Search button is displayed or not, which obviously should return true in this case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from selenium import webdriver #open the chrome browser driver = webdriver.Chrome() #open the google website driver.get("https://www.google.com") #find the search button xpath search_box = driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div[1]/div[3]/center/input[1]") print(search_box.is_displayed()) |
The preceding code uses the isDisplayed() method to determine if the element is displayed on a web page.
The preceding code returns true for the Google Search button.
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 Search button is enabled or not, which obviously should return true in this case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from selenium import webdriver #open the chrome browser driver = webdriver.Chrome() #open the google website driver.get("https://www.google.com") #find the search button xpath search_box = driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div[1]/div[3]/center/input[1]") print(search_box.is_enabled()) |
The preceding code uses the isEnabled() method to determine if the element is displayed on a web page. The preceding code returns true for the Google Search button.
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 WebElements. 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 15 16 17 18 |
from selenium import webdriver #open the chrome browser driver = webdriver.Chrome() #open the google website driver.get("https://www.google.com") #find the search button xpath search_box = driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div[1]/div[3]/center/input[1]") print(search_box.is_selected()) |
The preceding code uses the isSelected() method. It returns false for the Google Search button, because this is not a radio button, options in select, or a checkbox.
So now this is the complete code for Python Selenium is_displayed and is_enabled Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from selenium import webdriver #open the chrome browser driver = webdriver.Chrome() #open the google website driver.get("https://www.google.com") #find the search box input xpath search_box = driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div[1]/div[3]/center/input[1]") print(search_box.is_displayed()) print(search_box.is_enabled()) print(search_box.is_selected()) |
Also you can watch the complete video for Python Selenium is_displayed and is_enabled Method
Subscribe and Get Free Video Courses & Articles in your Email