Finding a <p> text which contains <br>

Hi

I am trying to locate using exists? a

in my page which is seperated
by two
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:

The Promotional Code you have entered has not been recognised.

Please check your Promotional Code and try again.

In IRB:

browser.p(:index => 7).text
“The Promotional Code you have entered has not been recognised\n\nPlease check
your Promotional Code and try again.”

In IRB:

browser.p(:text => “The Promotional Code you have entered has not been
recognised.\n\nPlease check your Promotional Code and try
again.”).exists?
false

2nd attempt:

browser.p(:text => “The Promotional Code you have entered has not been
recognised.

Please check your Promotional Code and try
again.”).exists?
false

3rd attempt:

browser.p(:text => “The Promotional Code you have entered has not been
recognised.

Please check your Promotional Code and try
again.”).exists?

Any help much appreciated.

Thanks in advance

false

On Tue, Jan 17, 2012 at 6:31 PM, Brad S. [email protected] wrote:

I am trying to locate using exists? a

in my page which is seperated
by two
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:

The Promotional Code you have entered has not been recognised.

Please check your Promotional Code and try again.

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

Preparation, parsing HTML into a DOM:

$ irb -r nokogiri
irb(main):001:0> doc = ‘

empty

foo
bar’
=> “

empty

foo
bar”
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”>]>]>]>]>]>

's with nested
:

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
=> [“

foo
bar

”]

All

's:

irb(main):009:0> dom.xpath(‘//p’).map &:to_s
=> [“

empty

\n”, “

foo
bar

”]

Kind regards

robert