How can we locate the value displaying in a p tag in Selenium web driver

In Selenium WebDriver I am trying to locate some value $2,772 /month displaying in a p tag. I have tried the below, but while running the test I am getting following error :

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//div[@id=’calculatedresult’]//div[@class=’_3Kmts’]//div[@class=’ant-row _2kHnl’]//div[@class=’z-k7K ant-col-xs-24′]//p[@class=’_3TRls _2fRgA’]”} (Session info: chrome=84.0.4147.135)

Selenium version: selenium-java-4.0.0-alpha-6

WebElement resultElement = driver.findElement(By.xpath("//div[@id='calculatedresult']//div[@class='_3Kmts']//div[@class='ant-row _2kHnl']//div[@class='z-k7K ant-col-xs-24']//p[@class='_3TRls _2fRgA']"));
String resultText = resultElement.getText();
System.out.println("Amount:"+resultText);

<div class="XE8MI" id="calculatedresult">
  <div class="_3Kmts">
    <div class="ant-row _2kHnl">
      <div class="ant-col-xs-24 ant-col-sm-24 ant-col-md-12">
        <p class="_3TRls _145-y">
          <!-- react-text: 559 -->How much will you
          <!-- /react-text -->
          <!-- react-text: 560 -->&nbsp;
          <!-- /react-text --><span class="_2O2nq">pay each month?</span></p>
      </div>
      <div class="z-k7K ant-col-xs-24 ant-col-sm-24 ant-col-md-12">
        <p class="_3TRls _1QZuL">
          <!-- react-text: 564 -->Your
          <!-- /react-text -->
          <!-- react-text: 565 -->&nbsp;
          <!-- /react-text --><span class="_34T3z">estimated repayments</span>
          <!-- react-text: 567 -->
          <!-- /react-text -->
          <!-- react-text: 568 -->could be
          <!-- /react-text -->
        </p>
        <p class="_3TRls _2fRgA">
          <!-- react-text: 570 -->$
          <!-- /react-text -->
          <!-- react-text: 571 -->2,772
          <!-- /react-text -->
          <!-- react-text: 572 -->&nbsp;
          <!-- /react-text --><span class="_3VTnt"><!-- react-text: 574 -->/ month<!-- /react-text --><sup>*</sup></span></p>
        <p class="_3TRls _2w2vj">
          <!-- react-text: 577 -->At an interest rate of
          <!-- /react-text -->
          <!-- react-text: 578 -->2.00
          <!-- /react-text -->
          <!-- react-text: 579 -->% over
          <!-- /react-text -->
          <!-- react-text: 580 -->30
          <!-- /react-text -->
          <!-- react-text: 581 -->years
          <!-- /react-text -->
        </p>
      </div>
    </div>
    <div class="ant-row _3jxXH">
      <div class="ant-col-xs-24 ant-col-sm-24 ant-col-md-16"></div>
      <div class="ant-col-xs-24 ant-col-sm-24 ant-col-md-8">
        <div class="_1A1_k"></div>
      </div>
    </div>
    <p class="_3TRls _2wwQC">
  </div>
</div>

Answer

Based on HTML snippet you have shared ( Not a good idea, always share in text). I can see issue with your xPath. For div class starting with z-k7K ant-, you are not giving full class name but have used = operator. Instead use contains.

Use :

WebElement resultElement = driver.findElement(By.xpath("//div[@id='calculatedresult']//div[@class='_3Kmts']//div[@class='ant-row _2kHnl']//div[contains(@class,'z-k7K ant-col-xs-24')]//p[@class='_3TRls _2fRgA']"));

Also I hope you dont have any issue with element load, if so use any of wait method for your element to be loaded completely.

Leave a Reply

Your email address will not be published. Required fields are marked *