On Tue, Jan 17, 2012 at 6:31 PM, Brad Symons <snomys / hotmail.com> wrote:
> I am trying to locate using exists? a <p> in my page which is seperated
> by two <br> tags, as shown below. In the IRB, these show as \n when I
> retrieve the text, but using that same value to locate it is not
> working.
>
> The html:
> <p>The Promotional Code you have entered has not been
> recognised.<br><br>Please check your Promotional Code and try again.<p>

You can do this with XPath expression "//p[br]":

Preparation, parsing HTML into a DOM:

$ irb -r nokogiri
irb(main):001:0> doc = '<r><p>empty<p>foo<br>bar</r>'
=> "<r><p>empty<p>foo<br>bar</r>"
irb(main):002:0> dom = Nokogiri.HTML(doc)
=> #<Nokogiri::HTML::Document:0x837ab38 name="document"
children=[#<Nokogiri::XML::DTD:0x837a976 name="html">,
#<Nokogiri::XML::Element:0x837a598 name="html"
children=[#<Nokogiri::XML::Element:0x837a494 name="body"
children=[#<Nokogiri::XML::Element:0x837a37c name="r"
children=[#<Nokogiri::XML::Element:0x837a23c name="p"
children=[#<Nokogiri::XML::Text:0x837a124 "empty">]>,
#<Nokogiri::XML::Element:0x837a020 name="p"
children=[#<Nokogiri::XML::Text:0x8379efe "foo">,
#<Nokogiri::XML::Element:0x8379e7c name="br">,
#<Nokogiri::XML::Text:0x8379d64 "bar">]>]>]>]>]>

<p>'s with nested <br>:

irb(main):007:0> dom.xpath('//p[br]')
=> [#<Nokogiri::XML::Element:0x837a020 name="p"
children=[#<Nokogiri::XML::Text:0x8379efe "foo">,
#<Nokogiri::XML::Element:0x8379e7c name="br">,
#<Nokogiri::XML::Text:0x8379d64 "bar">]>]
irb(main):008:0> dom.xpath('//p[br]').map &:to_s
=> ["<p>foo<br>bar</p>"]

All <p>'s:

irb(main):009:0> dom.xpath('//p').map &:to_s
=> ["<p>empty</p>\n", "<p>foo<br>bar</p>"]

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/