0
.//*[preceding::*[text()='Taco Salad'] and following::*[text()='Fajita Salad']]

I have text on this page and both queries for .//*[text()='Taco Salad'] and .//*[text()='Fajita Salad'] return what I expected. But I am a bit confused how to try to combine these with following and preceeding to grab some of the options nodes that present in the middle.

Does anyone have any working examples of using two axes?

I found this thread: combining XPATH axes (preceding-sibling & following-sibling) and tried to model my axes after it, but they don't seem to be valid xapths. Am I missing something obvious?

  • 1
    This xpath `//*[following-sibling::*[@class="bottom-notice"] and preceding-sibling::*[@name="new-answer"]]` seems to be working on this question page(works with selenium as well) – Kamal Mar 06 '19 at 02:18
  • Wow that worked for me TY, write this as an answer instead of a comment and I'll mark it as solving the problem. –  Mar 08 '19 at 21:31

2 Answers2

0

Unfortunately you haven't really told us what you want to achieve. Yes, you can use multiple axes; you can combine them in various different ways, depending on what result you want. So you need to explain what result you want. Using "and" at the top level simply tests whether both operands of the "and" select something.

Your question "does anyone have any working examples" isn't going to help. Yes, we can give you thousands of working examples, but it's entirely possible that none of them does what you want.

Here are some possible ways of combining two axis steps X and Y:

X/Y - select X, and from there, select Y

X and Y - return true if both X and Y select something

X or Y - return true if either X or Y selects something

X | Y - return the union of what X and Y select

X intersect Y - return the intersection of what X and Y select

The nearest you have come to a requirements statement is "grab some of the options nodes that present in the middle". That's hard to interpret without seeing your source document. It also might turn out to be a query that's easier in XPath 2.0 than in 1.0, so you really need to tell us which version you are using.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • The problem is huge, I could spend all day explaining its details, but I don't need that really. Thanks for the reply and the examples X/Y etc. I dont really need thousands of working examples and I dont need SO to solve my exact problem but you might be surprised how few syntactically correct examples of using two axes together exist online. "grab ... present in the middle" This is exactly what I was trying to do, but selenium (1.0xpath? iirc) returned invalid xpath. If you made a guide with syntactically correct examples of the X/Y examples that really all I was looking for. –  Mar 05 '19 at 22:56
  • Personally I find the approach of "search for an example that solves my problem" is a very inefficient way of working. It's much better to get a book that covers the capabilities of the language and skim read it cover to cover, then come back to relevant sections when you've got an overview of the capabilities. – Michael Kay Mar 06 '19 at 10:22
0

A working example of combining axes in single xpath can be:

//*[following-sibling::*[@class="bottom-notice"] and preceding-sibling::*[@name="new-answer"]]

It looks for the element form to write the answer on this page.

Kamal
  • 2,384
  • 1
  • 13
  • 25