Is there a DRYer XPath expression to concatenate?

This works well for finding button-like HTML elements (specially simplified):

  //button[text()='Buy']
| //input[@type='submit' and @value='Buy']
| //a/img[@title='Buy']

Now I need to limit this to context. For example, the Buy button, which appears inside the marked field:

//legend[text()='Flubber']

And this works, (.. leads us to the containing set of fields):

  //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 a way to simplify this? Unfortunately, these kinds of things do not work:

//legend[text()='Flubber']/..//(
  button[text()='Buy']
| input[@type='submit' and @value='Buy']
| a/img[@title='Buy'])

(Note that this is for XPath in the browser, so XSLT solutions will not help.)

+5
source share
2 answers

From the comments:

Correcting slightly to get the A and not the IMG: self::a[img[@title='Buy']]. (Now if only “Buy” can be reduced

XPath 1.0:

//legend[text() = 'Flubber']/..
   //*[
      self::button/text()
    | self::input[@type = 'submit']/@value
    | self::a/img/@title
    = 'Buy'
   ]

EDIT: . :

//*[legend[text() = 'Flubber']]
   //*[
      self::button/text()
    | self::input[@type = 'submit']/@value
    | self::a/img/@title
    = 'Buy'
   ]
+2

:

//legend[text()='Flubber']/..//*[self::button[text()='Buy'] or 
                                 self::input[@type='submit' and @value='Buy'] or
                                 self::img[@title='Buy'][parent::a]]

:

( ) legend, "Flubber", 1) a button, "" 2) input , type, "submit", value, "" 3) img title "Buy" a.

+3

All Articles