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
legend
element having the text "Flubber" that are any of 1) abutton
element having the text "Buy" or 2) aninput
element having an attributetype
whose value is "submit" and an attribute namedvalue
whose value is "Buy" or 3) animg
having an attribute namedtitle
with a value of "Buy" and whose parent is ana
element.
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