I need some help on checking if the button is disabled , attaching the screen shot of dom for reference, tried isEnabled() function from WebDriver, but it's returning true.
Asked
Active
Viewed 3,061 times
1
undetected Selenium
- 183,867
- 41
- 278
- 352
user2405952
- 41
- 2
- 7
2 Answers
1
You can check if the element has disabled attribute. If it exists you will get String results, if not you will get null
WebElement button = driver.findElement(locator);
bool isDisabled = button.getAttribute("disabled") != null;
Guy
- 46,488
- 10
- 44
- 88
1
There are two way to check if the button is disabled as follows:
Using
try-catch{}:try { //css driver.findElement(By.cssSelector("fieldset.checkbox button.calvary-button[disabled]")); //xpath //driver.findElement(By.xpath("//button[@class='calvary-button' and contains(.,'Continue')][@disabled]")); System.out.println("Button is disabled"); } catch (NoSuchElementException e) { System.out.println("Button is enabled"); }Using
findElements()and and assert zero length response:if(driver.findElements(By.cssSelector("fieldset.checkbox button.calvary-button[disabled]")).size()>0) System.out.println("Button is disabled"); else System.out.println("Button is enabled");
undetected Selenium
- 183,867
- 41
- 278
- 352
-
This checks if the button exists, not if it disabled. – Guy Sep 26 '18 at 07:31
-
Exactly, if the button exists along with the _attribute_ **disabled** e.g. `[disabled]`. Sysouts are configurable. – undetected Selenium Sep 26 '18 at 07:34
