Home > Software design >  Selenium-C#: How to click on a button when it is clickable and not having the disabled attribute in
Selenium-C#: How to click on a button when it is clickable and not having the disabled attribute in

Time:05-04

I have a 'save' button which is only clickable when 4 text fields before are filled with text.

In the inspect there are 2 buttons. The Cancel (Abbrechen) button is always clickable, but the save (Speichern) button has an "disabled" parameter in HTML which makes the button not clickable. As soon as the button is clickable, the "Disabled" Parameter disappears.

<div >
  <!--!-->
  <!--!-->
  <button type="button"       _bl_65608b69-0cf8-4c0e-a2ad-634125333afc=""><span >Abbrechen</span></button>
  <!--!-->
  <!--!-->
  <button type="submit"  _bl_8df46032-6965-4980-8c8c-6d3881a66eea="" disabled="">
    <span >Speichern</span>
  </button>
</div>

I tried to use this selenium method:

var element = _webDriverWait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(buttonSelector)));
element.Click();

But this method doesnt help me as it jumps to element.Click() even when button is not clickable and then it throws an exception.

Is there any other way, to check if a button is clickable?


Update

I also tried:

bool elementEnabled = element.Enabled

It returns always true even when the button is not clickable

CodePudding user response:

Invoke click on the element with text Speichern when the disabled attribute is no more present inducing WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.mud-dialog-actions button[type='submit']:not(disabled)"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='mud-dialog-actions']//button[@type='submit' and not(@disabled)]"))).Click();
    
  • Related