如何检查 class 属性是否包含某些内容?

How to check if class attribute contains something?

我有两个按钮,上一个和下一个:

   By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[1]")

   By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[2]")

第一个转到上一页,第二个转到我列表的下一页。当我打开我的页面时,第一个按钮将被禁用,直到我到达另一个页面,或者如果我的列表很短或为空,则两个按钮都将被禁用。如果它们未被禁用,我需要单击这些按钮。 disabled 和 not 的唯一区别是 class 属性:

class="disabled btn btn-plain btn-default-hover"
class="btn btn-plain btn-default-hover"

那么如何检查按钮 class 属性是否包含 'disabled'?场景是这样的 - 如果第二个按钮处于活动状态,请单击 'next' 然后如果第一个按钮处于活动状态,请单击 'back'

假设您有两个按钮,其 ID 分别为 "button1" 和 "button2"..

你可以这样检查:

if(document.getElementById("button1").className.indexOf("disabled") > 1) {
  // what to do if disabled...
} else {
  // what to do if active...
}

当然,您也可以对另一个按钮执行相同的操作...

//el is the web element
if(el.getAttribute("class").split(" ").contains("disabled"))
{  
    //your code

}

通过使用jquery编写以下代码

var ClassAtttr = $("#YourElementId").attr("class");

 if (ClassAtttr.indexOf('Your Expression'))
 {
   //do somthing
 }

为什么不试试'.Enabled'

WebElement backButton = driver.FindElement('Your Locator');
if(backButton.Enabled)
{
  'Your action to be performed
}