Reputation: 43
I'm scripting a web app that has labels for input fields. EG:
<label for="surname">Family Name:</label>...<input id="surname" ...></input>
I'd like to be able to write an XPath expression that takes the text "Family Name:" to match the label, then takes the "for" attribute from the label and uses that to find the associated input tag by matching its "id" tag.
I can make this "work" as follows:
//label[contains(.,"Family Name:")]/following::input[1]
However, for this web app I think it would be more reliable to match for/id. (EG: How can I be sure that in all possible layouts the first input tag following the label is the one I want? And what happens if somewhere down the line this page is rendered using a right-to-left script?
My ultimate aim is to create a library function that we can use to write QA scripts in advance of web pages to test against, when all we have is a picture or document with an input field labelled "Family Name:" and no idea what id some programmer will ultimately assign to the field.
Upvotes: 0
Views: 200
Reputation: 48368
Maybe this is what you're looking for?
//input[@id = //label[contains(., "Family Name:")]/@for]
As for your ultimate goal, you may want to take a look at the XPath gem for Ruby. Some of what you're doing may already be implemented there. (Specifically, check out the library's HTML Helpers)
Upvotes: 1