我正在使用selenium自动化网站,在JAVA
中编写代码。我正在尝试抓住所附图片中的值40 images
。我只是想获得价值40
。
HTML
<div class="group-right">
<h1 class="ng-binding">Night Life Of China</h1>
<ul class="stats">
<li class="stats-list">
<span>
<span class="stats-list__value ng-binding">40</span> images
</span>
</li>
有人可以帮忙吗?
这就是我所做的,(超级错误的)
WebElement imgcount = driver.findElement(By.cssSelector("span:first-child"));
imgcount.getAttribute("value");
// System.out.println("No. of Images Displayed: " + value());
if (listImages.size() == imgcount.getText()) {
System.out.println("Image and Count Match");
System.out.println("listImages: " + listImages.size());
System.out.println("Number of Images says:" + imgcount.getText());
} else {
System.out.println("IMAGES DO NOT MATCH! Closing Browser Now!");
System.out.println("Number of Images says:" + imgcount.getText());
答案 0 :(得分:1)
实际上,您提供的cssSelector
定位器会定位父span
元素,这就是您获取全文的原因。您只需要找到span
类stats-list__value
,如下所示: -
WebElement imgcount = driver.findElement(By.cssSelector("span.stats-list__value"));
System.out.println("Number of Images says:" + imgcount.getText());