During your Selenium WebDriver software testing test case creation, frequently you need to verify that your targeted element is enabled or disabled on page of software web application before performing action on it. Selenium has built-in method isEnabled() to check if the element is enabled or disabled on software web application's page. Verification of element is disable and verification of element invisible is totally different for any software web application. Many times you need selenium check if button is disabled, checkbox is enabled or disabled.
Do you know how to check element is disabled in selenium webdriver? Element is disabled means it is visible but not editable and element is invisible means it is hidden. VIEW THIS EXAMPLE to know how to wait till element is visible on the page of software web application.selenium is Enabled()
isEnabled() selenium webdriver method will verify and return true if a specified element is enabled. Else it will return false. Here is a sample example for isEnabled selenium. The generic syntax to store and print an element's status value is as below.
boolean fname = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
System.out.print(fname);
We can use the selenium isEnabled() method with if condition to take action based on element's enabled status on the page of the software web application. The example will explain to you deeply about isEnabled() webdriver method. If you are using the Selenium IDE software testing tool then VIEW THIS POST to know how to wait for the targeted element to become editable.
How to check if element is disabled in selenium Java
You can use the .isEnabled() method to check if the element is disabled or enabled in Selenium Java. Here, are the list of elements where you can use the .isEnabled() method to check element's status.
How to check if button is disabled/enabled in Selenium?
Selenium .isEnabled() method is used to check if the button is disabled or enabled. This selenium method will check and return false if the button is found disabled and return true if the button is enabled.
How to check if a radio button is disabled/enabled in Selenium Java?
You can use the .isEnabled() method to check the radio button status. It will return true if the radio button is enabled and false if the radio button is disabled.
How to check if dropdown is disabled in Selenium?
.isEnabled() method will check and return true if the dropdown is enabled and return false if the dropdown is disabled.
Copy bellow given @Test method part of check element enabled status and replace it with the @Test method part of example given on THIS PAGE. (Note : @Test method is marked with pink color in that linked page).
@Test
public void test () throws BiffException, IOException, InterruptedException
{
boolean fname = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
System.out.print(fname);
WebElement firstname = driver.findElement(By.xpath("//input[@name='fname']"));
WebElement lastname = driver.findElement(By.xpath("//input[@name='lname']"));
//Verify First name text box is enabled or not and then print related message.
if(firstname.isEnabled())
{
System.out.print("\nText box First name is enabled. Take your action.");
}
else
{
System.out.print("\nText box First name is disabled. Take your action.");
}
//Verify Last name text box is enabled or not and then print related message.
if(lastname.isEnabled())
{
System.out.print("\nText box Last name is enabled. Take your action.");
}
else
{
System.out.print("\nText box Last name is disabled. Take your action.");
}
}
Above given example, will check the status of First name and Last name text box and print a message in the console based on element is enabled or disabled. Same way selenium check if button is disabled or enabled using the selenium is enabled method.
boolean bnext = driver.findElement(By.cssSelector("btn.btn-default.btn-next")).isEnabled();
ReplyDeleteSystem.out.println(bnext);
if (bnext.isEnabled())
Eclips always show: Cannot invoke isEnabled() on the primitive type boolean
It should be
Deleteif(bnext == true)
// As the return type is Boolean, and it will only send True or False.
That's because you defined bnext as a boolean. You can't check .isEnabled() on a boolean. Define bnext as a WebElement and it will work with some other minor changes. Change your code to the below...
ReplyDeleteWebElement bnext = driver.findElement(By.cssSelector("btn.btn-default.btn-next"));
System.out.println(bnext.isEnabled());
if (bnext.isEnabled())