按钮的HTML。
<div id="MainTab" aria-disabled='true'>
当按钮不可点击时,
aria-disabled = 'true'
为:
<div id="MainTab" aria-disabled='true'>
当按钮可点击时,
aria-disabled
不存在:
<div id="MainTab">
如何编写代码来识别
aria-disabled
属性是否存在于:
<div id="MainTab" ...>
有人可以帮我吗?
鉴于 HTML:
<div id="MainTab" aria-disabled="true">
aria-disabled
是否存在,您可以使用以下任一种Locator Strategies:
使用CssSelector:
IWebElement Element = driver.FindElement(By.CssSelector("div#MainTab[aria-disabled]"));
使用XPath:
IWebElement Element = driver.FindElement(By.XPath("//div[@id='MainTab' and @aria-disabled]"));
实现方式是在元素上使用
.GetAttribute("aria-disabled")
返回值。
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
string ariaDisabled = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("MainTab"))).GetAttribute("aria-disabled");
ariaDisabled | HTML |
---|---|
“真实” |
|
“” |
|
如果是我,我会不止一次地使用它,我会把它放在一个函数中,例如
/// <summary>
/// Determines if the specified element has the aria-disabled attribute
/// </summary>
/// <param name="locator">The locator of the desired element</param>
/// <returns>true if the element has the aria-disabled attribute, false otherwise.</returns>
public bool IsAriaDisabled(By locator)
{
return "true" == new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(locator)).GetAttribute("aria-disabled");
}
然后你称它为
bool isAriaDisabled = IsAriaDisabled(By.Id("MainTab"));