英文:
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 -->
<!-- /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 -->
<!-- /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 -->
<!-- /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("//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);
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<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>
<!-- 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("//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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论