-1

Need to get the XPATH of following element in the table: Here the value 5000 is varying but Unit Voltage is constant text, need to get an xpath with following element concept, had tried below xpath, but its not working :

//div[contains(text(),'')]/follows/div/span[contains(text(),'Unit voltage (V)')]

The Concerns are there ID changes for each environment so cant be used, need help in getting working xpath on above format.

PFB the HTML code part

<tr class="v-formlayout-row v-formlayout-firstrow">
  <td class="v-formlayout-captioncell">
    <div class="v-caption v-caption-tiny v-caption-smalllabel v-caption-hasdescription">
      <span id="gwt-uid-207" for="gwt-uid-208">Unit voltage (V)</span>
    </div>
  </td>
  <td class="v-formlayout-errorcell">
    <div class="v-formlayout-error-indicator">
    </div>
  </td>
  <td class="v-formlayout-contentcell">
    <div class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w" id="gwt-uid-208" aria-labelledby="gwt-uid-207" style="">5000&nbsp;&nbsp;
    </div>
  </td>
</tr> 
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

0

Try this -

//span[contains(text(),'Unit voltage')]//ancestor::td//following::td[contains(@class,'contentcell')]//div

Or

//*[text()='Unit voltage']//following::td[contains(@class,'contentcell')]//div

Both should work.

These will return you div element, use getText() method to extract the text.

0

You can use the below given xpaths for getting the value 5000.

Option 1:
//tr//child::td[contains(@class,'contentcell')]//child::div[contains(@id,'gwt-uid')]

Option 2:
//td[contains(@class,'contentcell')]//child::div[contains(@id,'gwt-uid')]
Zohair
  • 268
  • 2
  • 7
0

To extract the text 5000 you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following solutions:

  • xpath 1:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Unit voltage (V)']//following::td[2]/div"))).getText());
    
  • xpath 2:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Unit voltage (V)')]//following::td[2]/div"))).getText());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

Thanks all for your valuable inputs, Below xpath worked for me, as there were multiple elements and the element order is same across environments used as below:

(//tr//child::td[contains(@class,'contentcell')]//child::div[contains(@id,'gwt-uid')])[7]