How to check if element contains specific class attribute

前端 未结 7 1742
生来不讨喜
生来不讨喜 2021-02-05 02:09

How can I check if a selenium web element contains a specific css class.

I have this html li element

  • 7条回答
    •  鱼传尺愫
      2021-02-05 02:18

      Improving on @uesports135 answer, "classess" should be a String array.

       public boolean hasClass(WebElement element, String htmlClass) {
              String[] classes = element.getAttribute("class").split("\\s+");
              if (classes != null) {
                  for (String classAttr: classes) {
                      if (classAttr.equals(htmlClass)) {
                          return true;
                      }
                  }
              }
              return false;
          }
      

    提交回复
    热议问题