Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if button is read Only or enabled in Selenium Using Java

Tags:

java

selenium

I am trying to find the button whether it is Read Only or Enabled (Clickable) in the page. After doing some action. I need to check this in if else condition so kindly help me to find the solution.

below are my html code of the button

When the Button is Read Only

<div id="agent_delete" 
     class="btn btn_own" 
     ng-confirm-click="Are you sure you want to delete selected Agent(s)?" 
     confirm-click-title="Delete Agent" 
     confirmed-click="delete()" 
     ng-disabled="!ctrlService.canDelete || gridApi.selection.getSelectedRows().length == 0" 
     disabled="disabled">

When the button is Enabled or clickable

<div id="agent_delete" 
     class="btn btn_own" 
     ng-confirm-click="Are you sure you want to delete selected Agent(s)?" 
     confirm-click-title="Delete Agent" 
     confirmed-click="delete()" 
     ng-disabled="!ctrlService.canDelete || gridApi.selection.getSelectedRows().length == 0">
like image 513
Dina Avatar asked Feb 23 '26 20:02

Dina


2 Answers

You have to use isEnabled(); method.

if(driver.findElement(By.id("agent_delete")).isEnabled()){
   //button is enabled
}
else{
//button is not enabled
}
like image 68
Lalindra Kawshika Avatar answered Feb 26 '26 10:02

Lalindra Kawshika


You could check the disabled attribute with getAttribute:

WebElement element = driver.findElement(By.id("agent_delete"));
if("disabled".equals(element.getAttribute("disabled"))) {
  // disabled
} else {
  // enabled
}

Or with a CSS selector with a single call:

if(driver.findElements(By.cssSelector("#agent_delete[disabled='disabled']")).size > 0) {
  // disabled
} else {
  // enabled
}
like image 27
Florent B. Avatar answered Feb 26 '26 09:02

Florent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!