Find element by text in Capybara

1 min


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

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

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
0
wtf
geek geek
1
geek
WIN WIN
0
WIN
Love Love
0
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

One Comment

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

Choose A Format
Trivia quiz
Story