Search code examples
htmlcssrubyseleniumwatir-webdriver

How to find element by watir-webdriver using more than one class?


For example, we have element like this: <div class="first_class second_class"></div>

So we can find using it's classes:

  • browser.div(class: 'first_class')
  • browser.div(class: 'second_class')

But what is about multiple search? Can I use combination of them?

  • browser.div(class: 'first_class second_class')?
  • browser.div(class: 'second_class first_class')?

Solution

  • In this case, use a CSS selector directly:

    browser.div(css: '.first_class.second_class')
    

    Note that the "by class" locator is actually transformed to a "by CSS selector" under the hood.