I\'m fairly new to XPath, been fiddling around with it for a few hours now, so i\'m not entirely sure if you can even do something like the following with it.
I had to read through your question a couple of times but I think I understand it. What you are interested in is predicates. Predicates allow you to pick nodes based on conditions.
For example, you could do:
//a[text()='second dropdown menu']/following::ul[1]/li/a[text()='link i want to find']
this would select any anchor with certain text in, find the next ul, then proceed through it's children.
Also, you can specify positional index within a result set, the following XPath is a demonstration (but it won't solve your problem):
//a[text()='first dropdown menu']/ul/li[last()]/a/text()
or you could use axes to navigate across siblings/ancestors/children:
//a[ancestor::ul/preceding::a[1]/text() = 'second dropdown menu']/text()
So I'm not sure I quite understood your question but this should help you write your XPath.
Basically, I'm assuming your XPath matches the anchor in multiple lists and you want to make sure you pick the right one. At some point in your XPath you need a predicate to specify a condition that will only be true for the list your desired node is in.
Correct me if i'm wrong - you need select "second drop down menu" from example above, but text can be duplicated, so you need only second one. XPath:
//a[text()='second drop down menu'][2]
just so i understand it correctly: you want the "some link" node after the "second dropdown menu"
ul[preceding::a[text()='second dropdown menu' and position()=last()]]/li/a[text()='link i want to find']
this should do the trick (I am not 100% sure that you will need to check for the position()=1, but I think that if you omit it, it will match all following ul since preceeding is "all" preceeding nodes - that depends on the rest of your xml structure)