Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting element with multiple classes in Capybara

I'm writing automation code in Capybara with Selenium. I have the following element in my HTML, and I wanna click this element in Capybara.

<a href="#" class="classA classB">click me</a>

At the moment, the way worked is something like following.

find('.classA', :text=>"click me").click

But I wanna select the element from the names of the two classes like this

find('a.classA.classB').click
click_on('a.classA.classB')

I know we can get javascript code fired, but this is not smart.

page.execute_script('$("a.classA.classB").click()')
like image 712
Ryo Avatar asked Apr 05 '13 14:04

Ryo


People also ask

How do you target an element with multiple classes?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Which is used to apply multiple classes to an element?

We will use “add()” method to add multiple classes to an element dynamically. add(class_1, class_2, …): It is used to assign a class or multiple classes to an element inside HTML.

Can an item have multiple classes?

While an element can only have a single ID attribute, you can give an element several classes and, in some cases, doing so will make your page easier to style and much more flexible. If you need to assign several classes to an element, add the additional classes and simply separate them with a space in your attribute.

Can a div have multiple Classnames?

Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well.


1 Answers

You can search an element by xpath

based on your example, seems like the following should work

//div[contains(@class, 'classA') and contains(@class, 'classB')]

You could also use css

(:css, ".classA.classB")
like image 200
Amey Avatar answered Sep 20 '22 13:09

Amey