Beardy
Beardy

Reputation: 172

Selenium-C#: How to click on a button when it is clickable and not having the disabled attribute in Blazor Web Application

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 class="mud-dialog-actions">
  <!--!-->
  <!--!-->
  <button type="button" class="mud-button-root mud-button mud-button-text mud-button-text-default mud-button-text-size-medium mud-ripple button"      _bl_65608b69-0cf8-4c0e-a2ad-634125333afc=""><span class="mud-button-label">Abbrechen</span></button>
  <!--!-->
  <!--!-->
  <button type="submit" class="mud-button-root mud-button mud-button-text mud-button-text-success mud-button-text-size-medium mud-ripple button button--blue" _bl_8df46032-6965-4980-8c8c-6d3881a66eea="" disabled="">
    <span class="mud-button-label">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

Upvotes: 1

Views: 651

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

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();
    

Upvotes: 1

Related Questions