Konzy262
Konzy262

Reputation: 3087

XPath - preceding sibling contains an element check

I have the following simplified HTML from a webpage...

<div class="row import-entity">
    <div class="col-md-11">
        <div class="row">
            <div class="col-md-3">
                <span class="mandatory"></span>
            </div>
            <div class="col-md-3">
                <input id="ImportTemplateEntities_2918.Header" type="hidden"></input>
                <input id="ImportTemplateEntities_5923.Header"></input>
            </div>
            <div class="col-md-3">
                <input id="ImportTemplateEntities_1234.Column"></input>
            </div>
            <div class="col-md-3"></div>
        </div>
        <div class="row">
            <div class="col-md-3"></div>
            <div class="col-md-3">
                <input id="ImportTemplateEntities_9182.Header" type="hidden"></input>
                <input id="ImportTemplateEntities_2834.Header"></input>
            </div>
            <div class="col-md-3">
                <input id="ImportTemplateEntities_1243.Column"></input>
            </div>
            <div class="col-md-3"></div>
        </div>
    </div>
</div>

With a requirement for the following XPath selection criteria...

The following element from the HTML above matches the search criteria

<input id="ImportTemplateEntities_5923.Header"></input>

(Note that the numerical value in the ID is assigned dynamically, and won't be known to me when searching the document.)

Before adding any sort of preceding sibling check this is what I have....

//input[contains(@id,'ImportTemplateEntities')][contains(@id,'Header')][not(contains(@type,'hidden'))]

How would I edit this to add a condition for checking whether the preceding sibling div contains a span with class = mandatory?

Upvotes: 2

Views: 2275

Answers (1)

kjhughes
kjhughes

Reputation: 111491

This XPath,

//input[contains(@id,'ImportTemplateEntities')]
       [contains(@id,'Header')]
       [not(@type='hidden')]
       [../preceding-sibling::div[span/@class='mandatory']]

will select all input elements whose id attribute value contains the noted substrings and does not have a type attribute whose value is hidden and whose parent has a preceding sibling div element with a child span that has a class attribute with a value of mandatory, as requested.

Upvotes: 3

Related Questions