Selecting element with multiple classes in Capybar

2019-02-06 22:39发布

问题:

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()')

回答1:

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")