我们如何在Selenium Web Driver中定位显示在p标签中的值

huangapple go评论84阅读模式
英文:

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

问题

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"></p>
  </div>
</div>
英文:

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(&quot;//div[@id=&#39;calculatedresult&#39;]//div[@class=&#39;_3Kmts&#39;]//div[@class=&#39;ant-row _2kHnl&#39;]//div[@class=&#39;z-k7K ant-col-xs-24&#39;]//p[@class=&#39;_3TRls _2fRgA&#39;]&quot;));
String resultText = resultElement.getText();
System.out.println(&quot;Amount:&quot;+resultText);

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

<!-- end snippet -->

答案1

得分: 1

根据您分享的HTML片段(这并不是一个好主意,最好以文本形式分享)。我可以看出您的xPath存在问题。对于以*z-k7K ant-*开头的div类,您没有提供完整的类名,而是使用了等号操作符。建议改用contains函数。

使用:

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']"));

另外,我希望您的元素加载没有任何问题,如果有问题,请使用任何等待方法确保您的元素完全加载完成。

英文:

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(&quot;//div[@id=&#39;calculatedresult&#39;]//div[@class=&#39;_3Kmts&#39;]//div[@class=&#39;ant-row _2kHnl&#39;]//div[contains(@class,&#39;z-k7K ant-col-xs-24&#39;)]//p[@class=&#39;_3TRls _2fRgA&#39;]&quot;));

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.

huangapple
  • 本文由 发表于 2020年8月26日 09:33:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63589269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定