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()')
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.
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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With