Query html tag with XPath
https://stackoverflow.com/questions/932579
Question
I am writing the selenium test.
I have a label there "Assign Designer" and the select box followed right after the label. Unfortunetely, select box has the dynamic id and I can not query it by id or any other it's attribute.
Can I build the XPath query that returns "First select tag after text 'Assign Designer'"?
PS. Selenium supports only XPath 1.0
Solution
This would be something like:
//label[text() = 'Assign Designer']/following-sibling::select[1]
Note that:
- The // shorthand is quite inefficient, because it causes a document-wide scan. If you can be more specific about the label's position, I recommend doing so. If the document is small, however, this won't be a problem.
- Since I don't know much about Selenium, I used "label". If it is not a
<label>
, you should use the actual element name, of course. ;-) - be sure to include a position predicate (
[1]
, in this case) whenever you use an axis like "following-sibling
". It's easily forgotten and if it is, your expressions may produce unexpected results.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow