4

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 ?

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
Shubham.b007
  • 71
  • 1
  • 1
  • 7
  • 2
    What is your exact usecase? Why do you want to avoid `cssSelector` or `xpath`? – undetected Selenium Jul 06 '18 at 07:02
  • As, Xpath is created using relative path/position of elements so there are always chances that Xpath gets change if new elements are introduced in the web page and this will lead to unnecessary failure of testcases. If we talk in terms of performance then finding element is bit slow in Xpath. – Shubham.b007 Jul 06 '18 at 07:18
  • 4
    @Shubham.b007 You are supposed to understand absolute xpath, Relative Xpath never gets affected by introducing new element in webpage. Its always good practise to use Relative xpath. – Ishita Shah Jul 06 '18 at 07:21
  • Also, It would be great if you can add parent elements of Button tag. – Ishita Shah Jul 06 '18 at 07:22
  • Thanks @Ishita Shah understood. – Shubham.b007 Jul 06 '18 at 07:30

2 Answers2

11

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
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Ok @cruisepandey . Actually, i want to know is there any way to find it without xpath/cssSelector. – Shubham.b007 Jul 06 '18 at 07:22
  • in this case , I do not see any link , nor ID , name , class name (because of spaces ) , so IMO top 6 can't be used. I do not see any harm using css selector though. – cruisepandey Jul 06 '18 at 07:25
1

The class attribute of this element...

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

contains multiple classnames.

Using By.classname("btn btn-xs btn-custom") you won't be able to locate it as:

Find elements based on the value of the "class" attribute. If an element has multiple classes, then this will match against each of them. For example, if the value is "one two onone", then the class names "one" and "two" will match.

As per your requirement, avoiding the & you still can use classname but the element may not be the first/only match. As an example:

  • By.classname("btn") //would match all the elements which contains the classname btn
  • By.classname("btn-xs") //would match all the elements which contains the classname btn-xs
  • By.classname("btn-custom") //would match all the elements which contains the classname btn-custom

As an alternative, you can also use the following Locator Strategies:

  • cssSelector:

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

  • xpath

    By.xpath("//button[@class='btn btn-xs btn-custom']")

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352