Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between classic and xpath selectors

Tags:

ruby

xpath

watir

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.

like image 339
diesel Avatar asked Feb 14 '26 00:02

diesel


1 Answers

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
like image 52
Justin Ko Avatar answered Feb 15 '26 17:02

Justin Ko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!