0

I am using this command to extract p tag

session.all('.entry p')

Giving results

[#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[1]">, 
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[2]">, 
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[3]">, 
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[4]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[5]">, 
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[6]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[7]">, 
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[8]">, 
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[9]">, 
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[10]">]

Now i want to extract all text from p node, i know there is loop method to merge all paragraph text, is there any other way. capybara provide?

1 Answer 1

1

The result of #all is a Capybara::Result. The doc says:

A Result represents a collection of Node::Element on the page. It is possible to interact with this collection similar to an Array because it implements Enumerable [...]

Thus, you may interact with it as with a enumerable, it does not offer any method you're asking for.

You can do this to retrieve the concatenated content:

session.all('.entry p').map(&:text).join

According to your tag "web-scraping" I assume you're using capybara for web scraping, and not for testing. As capybara's main purpose is for testing it does not have a built-in method for what you're asking for.

If you're implementing a test though you should rather do something like this (I used RSpec here):

within('.entry') do
  expect(page).to have_text 'something'
end

Or, if you really need to be very specific about the location of the spec (which in most cases is unnecessary) you should test each element on it's own:

expect(session.all('.entry p')[0]).to have_content 'something'
expect(session.all('.entry p')[1]).to have_content 'something else'

And just as a last sidenote: For web scraping there are better options than capybara.

7
  • I am doing web scraping, crawling. Thanks Aug 17, 2016 at 9:56
  • so did i answer your question? if so, please accept this as the answer. if not, please elaborate on your specific use case. Aug 17, 2016 at 10:26
  • I am thinking of using regular expression to extract the text from all the paragraph. any idea on this? for time being your answer is quite usable no doubt. Aug 17, 2016 at 11:18
  • I don't know what you mean. I gave you a solution to extract all the text from all paragraphs. What are you trying to achieve exactly? Aug 17, 2016 at 11:21
  • Did not you use regular expression???. This can be used to extract all text inside paragraphs without loop through it. Aug 17, 2016 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.