HermanCodes
HermanCodes

Reputation: 99

Selecting a parent of a child element using XPath

Before I start in the source code I have included in this post I have replaced any sensitive data with a string of "x".

<div class="pod productPod icon-life clearFix nonwrap"
     id="xxxxxxxxxx"
     internalid="xxxxxxxxxx"
     data-properties-type="xxxxxxxxxx"
     data-properties-code="xxxxxxxxxx"
     data-properties-loaded="False"
     data-properties-url="xxxxxxxxxx"
     data-properties-showmaintenanceerrormessage="False"
     data-properties-product-type="xxxxxxxxxx">
  <span class="podTitle">Your policies</span>
  <div id="1" class="productPodInner" data-qa-productpod="">
    <span class="notificationBubble fullProdLarge" aria-hidden="true"></span>
    <h2><a data-feedback="" class="clearFix roundelLink" href="#productId0" data-roundel-id="0">
      <span class="productIconWrapper">
        <span class="productIcon">
        </span>
      </span>
      <span class="productData">
        <span class="productName" style="min-height: auto;">Protection</span>
        <span class="notificationBubble fullProd"></span>
      </span>
    </a></h2>

    <div class="productDetails podContent" id="#productId0" style="width: 1903px; left: -306.703px;">
      <div class="productDetailsInner clearFix">
        <h3>xxxxxxxxxx</h3>
        <!-- TODO:: Display this in the 2nd column-->
        <dl class="initialPolicyContainer detailsList" style="display: none;">
          <dt>Policy Number</dt>
          <dd class="initialPolicyNumber" data-qa-text="xxxxxxxxxx">xxxxxxxxxx</dd>
        </dl>
        <div class="clearFix variant3Container" style=""><div class="group-1-2 clearFix">
          <div class="column">
            <dl class="detailsList">
              <dt>Policy number</dt>
              <dd class="policyNumberVal">xxxxxxxxxx</dd>
              <dt>Term</dt>
              <dd>xxxxxxxxxx</dd>
            </dl>

So my goal here is to be able to select the top parent using an Xpath locator and then click on that with selenium.

On the website there is a pod with multiple roundel sections each containing its own policy. Normally I can select a roundel using an Xpath query that finds the roundel by the policyType(text) but if there are 2 policies with the same policyType then this Xpath locator will only find the first one.

Roundel has a unique policyNumberVal (line 29)

<dd class="policyNumberVal">xxxxxxxxxx</dd>

That I can locate using this Xpath:

//dd[@class='policyNumberVal' and text() = '{policyNumber}']

What I could like to know is, once I have located the 'policyNumberVal' how do I then step up and select the parent ?

Please let me know if you need any more information than this.

Upvotes: 2

Views: 2435

Answers (1)

kjhughes
kjhughes

Reputation: 111491

You can use the parent axis,

//dd[@class='policyNumberVal' and text() = '{policyNumber}']/parent::*

its abbreviation,

//dd[@class='policyNumberVal' and text() = '{policyNumber}']/..

or, elevate the predicate and select the parent directly:

//dl[dd[@class='policyNumberVal' and text() = '{policyNumber}']]

Upvotes: 1

Related Questions