Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a DRYer XPath expression for union?

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.)

like image 209
Chris Noe Avatar asked Apr 01 '11 17:04

Chris Noe


1 Answers

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) a button element having the text "Buy" or 2) an input element having an attribute type whose value is "submit" and an attribute named value whose value is "Buy" or 3) an img having an attribute named title with a value of "Buy" and whose parent is an a element.

like image 112
Wayne Avatar answered Oct 11 '22 18:10

Wayne