This works nicely for finding button-like HTML elements, (purposely simplified):
//button[text()='Buy']
| //input[@type='submit' and @value='Buy']
| //a/img[@title='Buy']
Now I need to constrain this to a context. For example, the Buy button that appears inside a labeled box:
//legend[text()='Flubber']
And this works, (.. gets us to the containing fieldset):
//legend[text()='Flubber']/..//button[text()='Buy']
| //legend[text()='Flubber']/..//input[@type='submit' and @value='Buy']
| //legend[text()='Flubber']/..//a/img[@title='Buy']
But is there any way to simplify this? Sadly, this sort of thing doesn't work:
//legend[text()='Flubber']/..//(
button[text()='Buy']
| input[@type='submit' and @value='Buy']
| a/img[@title='Buy'])
(Note that this is for XPath within the browser, so XSLT solutions will not help.)
Combine multiple conditions in a single predicate:
//legend[text()='Flubber']/..//*[self::button[text()='Buy'] or
self::input[@type='submit' and @value='Buy'] or
self::img[@title='Buy'][parent::a]]
In English:
Select all descendants of the parent (or the parent itself) for any
legendelement having the text "Flubber" that are any of 1) abuttonelement having the text "Buy" or 2) aninputelement having an attributetypewhose value is "submit" and an attribute namedvaluewhose value is "Buy" or 3) animghaving an attribute namedtitlewith a value of "Buy" and whose parent is anaelement.
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