Find element by text in Capybara


632
1 comment, 632 points
element by text in capybara
element by text in capybara

Find element by text in Capybara

Think about a simple situation  : You have an element but you’ve got not css selector , or any other type to identify that element ,  lucky you capybara user, you can find element by text in Capybara.

Peace of cake , Capybara offers the :text option to go along with your selector:

page.find('div', text: 'content')

There is the other option when you can find an element by text in capybara using regular expression:

page.find('div', text: /contents?/i)

 

Third option :

Find element by text using xpath

This solution you can apply is on any programming language , your locator will look something like this

"//*[contains(text(),'text')]"
Capybara click on text:
 # click on any text
    def click_on_text(text_to_click)
      find(:xpath, "//*[contains(text(),'#{text_to_click}')]").click
    end

Happy testing!

Ruby Capybara select random value from dropdown

Advertisements

Selenium performance tips

How to disable Chrome save password bubble for Selenium tests

 

How to use : Capybara get attribute


Like it? Share with your friends!

632
1 comment, 632 points
Test engineer

One Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.