简体   繁体   中英

xpath to find a text box, below a specific node which in turn contains specific sub node

I had a hard time creating a title for this one!

Below is the example HTML that I'm parsing - truncated in parts for brevity, but hopefully enough for someone to understand what I'm looking for.

I'm looking to identify the INPUT at the very bottom of the example code. I cannot use the ID because it constantly changes. ie id="id_mstr111_txt"

Let me describe in english, as best as I can, what I'm trying to get at:

find the INPUT
...under a DIV with class="mstrPromptQuestion" that has as a descendant

...... span class="mstrPromptQuestionTitleBarTitle">Abstracted Period

Or written another way:

Given a DIV with class="mstrPromptQuestion" that contains a deeply embedded child node: span class="mstrPromptQuestionTitleBarTitle">Abstracted Period find the INPUT (without using the ID because the numbers change with every build)

I'm not sure if this is even possible with XPath 2.0 and no jquery - just BASIC xpath functions.

 <div id="id_mstr85" **class="mstrPromptQuestion"** style="display: block;"> <span class="mstrPromptQuestionRequired"> <div class="mstrPromptQuestionTitleBar"> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <td valign="middle" align="left"> <span class="mstrPromptQuestionTitleBarIndex" style="display: inline;">2.</span> <span **class="mstrPromptQuestionTitleBarTitle">Abstracted Period**</span> <span class="mstrPromptQuestionTitleBarRequired" title="(Required)">(Required)</span> </td> </tr> </tbody> </table> </div> <table class="mstrPromptQuestionInfoTable" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="mstrPromptQuestionInfoCellLeft"> <td class="mstrPromptQuestionInfoCellRight"> </td> </td> </tr> </tbody> </table> <div class="mstrPromptQuestionContents"> <table class="mstrPromptQuestionSimpleAnswerViewTitle" style="display: none;" cellspacing="0" cellpadding="0"> <div class="mstrPromptQuestionSimpleAnswerView" style="padding-left: 0px;" onmousedown="this.previousSibling.rows[0].cells[0].childNodes[0].checked = true; mstr.behaviors.PromptQuestion.onClickRadio('id_mstr85', false)"> <table id="id_mstr89" class="mstrListCart" style="display: table;" cellspacing="0" cellpadding="0"> <colgroup> <tbody> <tr> <td class="mstrListCartCellAvailableHeader"> <table class="mstrListCartTablePathView" cellspacing="0" cellpadding="0"> <div id="id_mstr108" class="mstrSearchField" title=" style=" display: block;=""> <table class="mstrSearchFieldTable" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="mstrSearchFieldSearchBox"> <div id="id_mstr111" class="mstrTextBoxWithIcon" title=" style=" display: block;=""> <div class="mstrTextBoxWithIconCaption"> <table class="mstrTextBoxWithIconTable" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="mstrTextBoxWithIconCellInput"> <div><**input** id="id_mstr111_txt" maxlength="" onclick="if (mstr.utils.ISW3C) {this.focus();}" onkeypress="return mstr.behaviors.TextBoxWithIcon.onkeypress(arguments[0], self, 'id_mstr111', this.value)" name="id_mstr111_txt" style="background-color: rgb(255, 255, 255);" size="" type="text" /></div> </td> 

For described in english:

//div[@class='mstrPromptQuestion'][.//span[@class='mstrPromptQuestionTitleBarTitle']]//input[contains(@id, '_txt')]

Written another way is the same, only that you specify to not use the id.

Identify a unique parent based on an attribute or based on a child and continue from there.

For above xpath you can also add a text constraint to the span like

 [contains(text(), 'Abstracted Period')]

resulting in something like:

//div[@class='mstrPromptQuestion'][.//span[@class='mstrPromptQuestionTitleBarTitle'][contains(text(), 'Abstracted Period')]]//input[contains(@id, '_txt')]

If you're sure it's the last input in the div , you could do something like:

(//div[@class='mstrPromptQuestion']//input)[last()]

or something like:

(//div[@class='mstrPromptQuestion' and .//span[@class='mstrPromptQuestionTitleBarTitle']='Abstracted Period']//input)[last()]

if mstrPromptQuestionTitleBarTitle is important.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM