8

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()')
Ryo
  • 2,003
  • 4
  • 27
  • 42

1 Answers1

15

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")
Amey
  • 8,470
  • 9
  • 44
  • 63
  • Thank you for your response, but still getting the same error. Capybara::ElementNotFound: Unable to find link or button "//*[contains(concat(' ',@class,' '),'classA') and (contains(concat(' ',@class,' '),'classB')) ]" – Ryo Apr 05 '13 at 15:22
  • @Ryo, made an edit, try that out. If that doesn work we can try some thing else. – Amey Apr 05 '13 at 15:29
  • Thank you!The second one worked, actually I did like this find(:css, ".classA.classB", :visible=>true).click – Ryo Apr 05 '13 at 16:05
  • @Ryo, Great!! Just in case you have time and are as curios as me, can you check for the first option as well? – Amey Apr 05 '13 at 16:33