isDisplayed, isSelected, isEnabled in Selenium
This tutorial describes how to check the state of a WebElement.
In this tutorial, we will learn isDisplayed, isEnabled, isSelected method in Selenium, and how to check the state of a WebElement. There are many methods which are used to determine the visibility scope for the web elements – isSelected(), isEnabled() and isDispalyed().
Many a time a test fails when we click on an element or enter text in a field. This is because the element is displayed or exist in DOM, but it does not exist on the web page.
WebDriver facilitates the user with the following methods to check the visibility of the web elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels etc.
- isDisplayed()
- isSelected()
- isEnabled()
- Boolean isSelected(): This method determines if an element is selected or not. It returns true if the element is selected and false if it is not. It is widely used on check boxes, radio buttons and options in a select.
2. Boolean isDisplayed(): This method determines if an element is displayed or not. It returns true if the element is displayed and false if it is not. Advantage of this method is that it avoids parsing an elements style attribute.
3. Boolean isEnabled(): This method determines if an element is enabled or not. It returns true if element is enabled (All elements apart from disabled input elements) and false if otherwise.
Steps to follow to understand when an element isEnabled and isDisplayed.
- Launch the web browser and open the application under test – https://duckduckgo.com/
- Verify the web page title
- Verify if the “Search Box” is displayed
- Verify that the “Search Box” is enabled
- If Search Box is enabled, then search for text – Selenium
- Close the browser
- 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
public
class
VerifyConditionsDemo {
public
static
void
main(String[] args) {
System.setProperty(
"webdriver.gecko.driver"
,
"C:\\Users\\Vibha\\Software\\geckodriver-v0.26.0-win64\\geckodriver.exe"
);
WebDriver driver =
new
FirefoxDriver();
// launch the firefox browser and open the application url
// maximize the browser window
driver.manage().window().maximize();
// declare and initialize the variable to store the expected title of the
// webpage.
String expectedTitle =
"DuckDuckGo — Privacy, simplified."
;
// fetch the title of the web page and save it into a string variable
String actualTitle = driver.getTitle();
// compare the expected title of the page with the actual title of the page and
// print the result
if
(expectedTitle.equals(actualTitle)) {
System.out.println(
"Verification Pass- The correct title is displayed on the web page."
);
}
else
{
System.out.println(
"Verification Failed - An incorrect title is displayed on the web page."
);
}
// verify that the “Search" Box is displayed
WebElement searchBox = driver.findElement(By.id(
"search_form_input_homepage"
));
if
(searchBox.isDisplayed()) {
System.out.println(
"Search Box is visible. Return: "
+ searchBox.isDisplayed());
}
else
{
System.out.println(
"Search Box is not visible. Return: "
+ searchBox.isDisplayed());
}
// verify that the “Search” Box is enabled
if
(searchBox.isEnabled()) {
System.out.println(
"Search Box is enabled. Return: "
+ searchBox.isEnabled());
searchBox.sendKeys(
"Selenium"
);
}
else
{
System.out.println(
"Search Box is not enabled. Return: "
+ searchBox.isEnabled());
}
System.out.println(
"Successful Execution of Test."
);
// close the web browser
driver.close();
}
}
Output
Verification Pass- The correct title is displayed on the web page.
Search Box is visible. Return:
true
Search Box is enabled. Return:
true
Successfull Execution of Test.
REPORT THIS ADisSelected
Steps to follow to understand when an element is Selected or not.
- Launch the web browser and open the application under test – https://www.seleniumeasy.com/test/basic-radiobutton-demo.html
- Verify if first Radio Button (Male) is selected or not
- If first radio button is already selected, then select Radio Button 2, else vice versa.
- Print the value of selected Radio Button
- Print the value of selecting a Radio Button
- Close the browser
- 1234567891011121314151617181920212223242526272829303132333435363738394041424344
import
java.util.List;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
public
class
isSelectedDemo {
public
static
void
main(String[] args) {
System.setProperty(
"webdriver.gecko.driver"
,
"C:\\Users\\Vibha\\Software\\geckodriver-v0.26.0-win64\\geckodriver.exe"
);
FirefoxDriver driver =
new
FirefoxDriver();
driver.manage().timeouts().implicitlyWait(
60
, TimeUnit.SECONDS);
List<WebElement> Radio_Options = driver.findElements(By.name(
"optradio"
));
// Create a boolean variable which will hold the value (True/False)
boolean
radio_value =
false
;
// This statement will return True, in case of first Radio button is selected
radio_value = Radio_Options.get(
0
).isSelected();
// Identify if Radio Button 1 is selected or not. If Button 1 is already
// selected, then select Button 2
if
(radio_value ==
true
) {
Radio_Options.get(
1
).click();
System.out.println(
"Button Selected is :"
+ Radio_Options.get(
1
).getAttribute(
"value"
));
}
else
{
Radio_Options.get(
0
).click();
System.out.println(
"Button Selected is :"
+ Radio_Options.get(
0
).getAttribute(
"value"
));
}
driver.findElement(By.id(
"buttoncheck"
)).click();
String Button_Selected = driver.findElement(By.xpath(
"//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]"
))
.getText();
System.out.println(
"Get Checked Value is :"
+ Button_Selected);
// close the web browser
driver.close();
}
}
Comments