how to trim or ignore leading and ending spaces in [text()='...'] block of the xpath expression?

2,740 views
Skip to first unread message

ejohn

unread,
Jun 10, 2008, 8:36:38 AM6/10/08
to selenium-users...@googlegroups.com
Hi,
If someone has already come accross with the following problem, please share your expirence!

I locate elements by complicated xpath expressions. Something like this:

//table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF_form_edBatchNbr_gr_headerT']/thead/tr/td[*text()='Status'*]

The problem is that in UI text of this element changes dynamically - sometimes a space is added to the end of the string and *"Status"* changes to *"Status "* so I need to convert *"Status "* to *"Status"* as [*text()='Status'*|/message/*text()='Status'*] is no more applicable and selenium can not find the element.

So the question: is there any mechanism that allows to ignore leading and ending spaces in [*text()='...'*|/message/*text()='...'*] block of the xpath expression?
Thanks in advance.

selvan

unread,
Jun 11, 2008, 12:49:00 AM6/11/08
to selenium-users...@googlegroups.com
If you use //table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td[*text()='Status'*] expression, it will check td has text "Status". Instead you could use //table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td in getText of selenium deriver that will return you td content. If you have content, you could trim it at selenium driver end.

String content = selenium.getText("//table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td")
assertEquals(*"Status", content.trim()*)

ejohn

unread,
Jun 11, 2008, 7:55:01 AM6/11/08
to selenium-users...@googlegroups.com
Selvan, thanks for advice, it matches the problem but does not complitely cover it:
Several nodes have the same xpath:
//table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td
So i would not prefer to hardcode them like this:
//table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td
//table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td[2]
//table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td[3]
...
The best way is to distinguish tjem by text value, like this:
"//table[@id='ctl00_phF_form_edBatchNbr_gr']/tbody/tr/td/div/table[@id='ctl00_phF _form_edBatchNbr_gr_headerT']/thead/tr/td[text()='" + textValue + "' ]"
The proble is that in UI displayed text has some extra spaces at the end. So I need some kind of regexp on server side.
[text().contains('" + textValue + "') ] - something like this....

Is it also possible to use regexps for locators?
Example:
//td[@id='usrCaption_tlbTools_but2'] -> //td[@id.contains('usrCaption_tlbTools_but2']) something like this...\\

ejohn

unread,
Jun 16, 2008, 12:45:57 AM6/16/08
to selenium-users...@googlegroups.com
Thanks a lot!
That's really the thing I needed!

Reply all
Reply to author
Forward
0 new messages