0

I'm currently developing some tests using Selenium RC. I have to problem regarding selenium Xpath "OR" statement with xpath input and textareas. Getting to the point. I have designed 2 xpaths

textarea: //span[text()='{0}']/parent::*/following-sibling::*//textarea

input: //span[text()='{0}']/parent::*/following-sibling::*//input

Generally im using Selenium.Type(myXpath,"label=myValue"); and both of those xpaths works ok. Problem is when i was trying to merge them. I doesn't really make a difference to me if its an input or a textarea since both methods do exacly the same thus i tried to merge them with | (or) statement.

//span[text()='{0}']/parent::*/following-sibling::*//textarea|//input

but unfortunately it doesn't work. If the order is //textarea|//input it matches textarea and if //input|//textarea it matches inputs. I was trying various combinations and still nothing. Am i missing brackets or some required syntax? Thank you for all the help.

Ross Patterson
  • 9,527
  • 33
  • 48
Shuffler
  • 173
  • 2
  • 11

1 Answers1

1

Fetch all nodes (*), then test the element names for the ones you want using predicates.

//span[text()='{0}']/parent::*/following-sibling::*//*[local-name() = 'textarea' or local-name() = 'input']

If your XPath engine has no support for local-name() (which seems to be a problem with Selenium, see comments), you will have to repeat the complete statement:

//span[text()='{0}']/parent::*/following-sibling::*//textarea | //span[text()='{0}']/parent::*/following-sibling::*//input

The query you've been trying to use is equivalent to

(//span[text()='{0}']/parent::*/following-sibling::*//textarea) | (//input)

Which also explains the results of switching textarea and input.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • Unfortunately it doesn't work for me. I'm getting an error: Selenium.SeleniumException : ERROR: Invalid xpath [3]: XPath parse error //span[text()='Uwagi']/parent::*/following-sibling::*//(textarea|input): // RelativeLocationPath // Expr – Shuffler Feb 21 '13 at 10:41
  • Sorry, missed we better should use XPath 1.0 for Selenium. Updated the answer, this one should work fine now. – Jens Erat Feb 21 '13 at 11:17
  • I'm getting server error, Not implemented yet: Xpath function local-name() – Shuffler Feb 21 '13 at 11:28
  • If you're not using namespaces, try using `name()` instead. If that's not working either, you will have to repeat the whole XPath statement after the `|`. – Jens Erat Feb 21 '13 at 11:32
  • Unfortunately name didn't work either. I see into Xpath namespaces and see if i can make it more elegant. For now i just used | with both xpaths. Thanks for your time and help. Cheers! – Shuffler Feb 21 '13 at 11:37
  • Try updating to the newest version and otherwise write a bug report to Selenium. ;) – Jens Erat Feb 21 '13 at 11:45
  • Can you give me some tips regarding updating to newest version? Should i update xpath in browser somehow? I'm sorry if this is a stiupid question im totally new in this. My Rc server is ver 2.30 if it matters. – Shuffler Feb 21 '13 at 12:15
  • Don't know selenium at all. Compare your version with the one on their website. Maybe they extended XPath support with some update. If they're using your browser's XPath engine, all you can do is update your browser (which is bad for testing old browser versions, of course). – Jens Erat Feb 21 '13 at 12:32