The isEnabled() function in selenium webdriver returns true always, when the webdriver is running on sites developed using Angular 6. All the answers relating to this topic that I found talked about the disabled not being an attribute and instead being written in the class of the button. Check this link for a detailed description.
Button enabled or disabled : How does webdriver decide?
But the code of the website that I am working on does not have it written in the class. Here is the code of my website.
<button _ngcontent-c61="" class="push-right-sm mat-raised-button mat-primary" color="primary" mat-raised-button="" disabled="">
<span class="mat-button-wrapper">SAVE</span><div class="mat-button-ripple mat-ripple" matripple=""></div><div class="mat-button-focus-overlay"></div></button>
How should I check if this element is enabled or not correctly?
Edit: Here is how I check it through my code.
WebElement button = driver.findElement(By.xpath("//button/span[contains(text(),'SAVE')]"));
if(button.isEnabled()){ System.out.println("The button is enabled."); }
Your locator is finding the SPAN
inside the BUTTON
and not the button itself. Try
//button[./span[.='SAVE']]
NOTE: You don't need to use contains()
in this case since the entirety of the text is "SAVE" so I removed contains()
.
This basically reads as find a BUTTON
that has a SPAN
that has contained text equal to "SAVE". I tested it on the HTML you provided and it's working.