How to find element that has multiple class in selenium

不羁的心 提交于 2021-02-18 08:05:25

问题


I have an element with 3 classes which I need to find with selenium

<button style="padding:2px 7px; background-color:#4caeea" 
  class="btn btn-xs btn-custom" </button>

I could not find it with : By.classname("btn btn-xs btn-custom")

I do not want to use xpath & cssSelector . What other options do I have ?


回答1:


This By.classname("btn btn-xs btn-custom") will not work, as it contains multiple spaces which means it is combination of 3 classes.

You will have to switch to css selector or xpath , I do not know why you have mentioned that you do not want to use both of them.

However, If you are interest to use css selector :

You can try this :

By.cssSelector("btn.btn-xs.btn-custom")  

If you go by precedence :

  1. ID
  2. name
  3. classname
  4. linkText
  5. partialLinkText
  6. tagName
  7. css selector
  8. xpath


来源:https://stackoverflow.com/questions/51204668/how-to-find-element-that-has-multiple-class-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!