HTML代码如下
<div class="a-row">
<a class="a-link-normal" title="1.0 out of 5 stars" href="/gp/customer-reviews/RBDVABUKMPJY8/ref=cm_cr_arp_d_rvw_ttl?ie=UTF8&ASIN=B071NZZHF9">
<i class="a-icon a-icon-star a-star-1 review-rating" data-hook="review-star-rating">
<span class="a-icon-alt">1.0 out of 5 stars</span>
</i>
</a>
<span class="a-letter-space"/>
<a class="a-size-base a-link-normal review-title a-color-base a-text-bold" data-hook="review-title" href="/gp/customer-reviews/RBDVABUKMPJY8/ref=cm_cr_arp_d_rvw_ttl?ie=UTF8&ASIN=B071NZZHF9">One Star</a>
</div>
我想使用父类='a-row'在span类中调用5星中的1.0。
有人可以帮助我们如何调用,因为必须使用partiallinktext方法调用,只使用partiallinktext = 5颗星。
我得到输出的方式如下:
List<WebElement> rstar = dr.findElements(By.xpath("//*[@id='cm_cr-review_list']//div[@class='a-row']//a[@class='a-link-normal']//i"));
String c;
for(WebElement erstar : rstar) {
c=erstar.getAttribute("innerText");
System.out.println(c);
感谢@AliAzam的帮助:)
首先找一下Span
elem:
WebElement spanElem = driver.findElement(By.className("a-row")).findElement(By.tagName("span"));
然后使用以下:
spanElem.findElement(By.partialLinkText("out of 5 stars"));
另一种方案:
List<WebElement> allParents = driver.findElements(By.className("a-row"));
for (WebElement elem : allParents) {
WebElement spanElem = elem.findElement(By.tagName("span"));
//System.out.println(spanElem.getText());
spanElem.findElement(By.partialLinkText("out of 5 stars"));
//System.out.println(spanElem.findElement(By.partialLinkText("out of 5 stars")).getText());
}
要么
List<WebElement> rstar = dr.findElements(By.className("a-row"));
for(WebElement erstar : rstar)
{
erstar.findElement(By.partialLinkText("out of 5 stars"));
String c = erstar.getText();
System.out.println(c);
}
希望它可以解决您的问题。