What the difference beetween next sentences?
frame.span(:text=>'Patient')
frame.span(:xpath=>".//span[text() = 'Patient']")
First sentence works well, but using second with xpath I can't find element on page.
Summary
The difference is in the normalizing of space.
If you set $DEBUG=true, you will see that Watir converts .span(:text=>'Patient') into the XPath:
.//span[normalize-space()='Patient']
As a result, there are different results when the text node has leading/trailing whitespace.
Examples
For example, in the following HTML, there is no leading/trailing whitespace:
<span>Patient</span>
As a result, both approaches return the same result:
p browser.span(:text=>'Patient').exists?
#=> true
p browser.span(:xpath=>".//span[text() = 'Patient']").exists?
#=> true
However, if we add some extra whitespace:
<span>Patient </span>
We see that the :xpath locator fails since it is looking for the text node to be exactly "Patient" not "Patient ". In contrast the :text locator will ignore the leading/trailing whitespace.
p browser.span(:text=>'Patient').exists?
#=> true
p browser.span(:xpath=>".//span[text() = 'Patient']").exists?
#=> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With