0

HTML of the button.

<div id="MainTab" aria-disabled='true'> 

when button is not clickable, aria-disabled = 'true' as:

<div id="MainTab" aria-disabled='true'> 

when button is clickable, aria-disabled is not present as:

<div id="MainTab">

How to write a code to identify whether aria-disabled attribute is present or not in:

<div id="MainTab" ...>

The element "MainTab" is always visible.

"aria-disabled" is true, "MainTab" is disabled, not clickable

"aria-disabled" is false, "MainTab" is enabled and clickable

How do I use wait.until() to probe either "MainTab" is disabled or clickable? The purpose is to probe when "MainTab" is clickable, then execute

FindElement(By.Id("MainTab")).click(); 
JeffC
  • 22,180
  • 5
  • 32
  • 55

2 Answers2

0

Given the HTML:

<div id="MainTab" aria-disabled="true">

To probe if the attribute aria-disabled is present or not you can use either of the following Locator Strategies:

  • Using CssSelector:

    IWebElement Element = driver.FindElement(By.CssSelector("div#MainTab[aria-disabled]"));
    
  • Using XPath:

    IWebElement Element = driver.FindElement(By.XPath("//div[@id='MainTab' and @aria-disabled]"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your help. Now I can get: string temp = driver.FindElement(By.Id("MainTab")).GetAttribute("aria-disabled"); The element "MainTab" is alawys visible. "aria-disabled" is ture, "MainTab" is disabled, not clickable "aria-disabled" is false, "MainTab" is enabled, and clickable How to use wait.until to probe either "MainTab" is disable or clickale? Or use wait.until(ToString) to get when "aria-disabled" is false? The purpose is to probe when "MainTab" is clickable, then execute FindElement(By.Id("MainTab")).click(); – Rejoice2023 Mar 07 '23 at 16:58
  • I do not how to add comments with the proper format. I pasted my comments under my original questions under: 2023-Mar-07 – Rejoice2023 Mar 07 '23 at 17:02
0

The way to do this is to use .GetAttribute("aria-disabled") on the element to return the value.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
string ariaDisabled = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("MainTab"))).GetAttribute("aria-disabled");
ariaDisabled HTML
"true" <div id="MainTab" aria-disabled='true'>
"" <div id="MainTab">

If it were me and I was going to use this more than once, I would put it in a function, e.g.

/// <summary>
/// Determines if the specified element has the aria-disabled attribute
/// </summary>
/// <param name="locator">The locator of the desired element</param>
/// <returns>A tuple of the element and a bool as true if the element has the aria-disabled attribute, false otherwise.</returns>
public (IWebElement, bool) IsAriaDisabled(By locator)
{
    IWebElement e = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(locator));
    return (e, "true" == e.GetAttribute("aria-disabled"));
}

and then you call it like

(IWebElement e, bool isAriaDisabled) = IsAriaDisabled(By.Id("MainTab1"));
if (isAriaDisabled)
{
    Console.WriteLine("'MainTab' is disabled, not clickable");
}
else
{
    Console.WriteLine("'MainTab' is enabled and clickable");
    e.Click();
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thank you for your answer. I do not know how to reply your answer in the proper format. I added my further question in my original question under 2023-Mar-06. – Rejoice2023 Mar 07 '23 at 17:07
  • I've updated my answer to address your comment – JeffC Mar 07 '23 at 17:25