简体   繁体   中英

How to call a element by partialinktext using its parent class in selenium webdriver

HTML code is as follows

<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>

I want to call 1.0 out of 5 stars in the span class using the parent class='a-row'.

Can someone help on how can we call as it has to be called using partiallinktext method using only partiallinktext= out of 5 stars.

First look for the Span elem like:

WebElement spanElem = driver.findElement(By.className("a-row")).findElement(By.tagName("span"));

Then use below:

spanElem.findElement(By.partialLinkText("out of 5 stars"));

Another Solution:

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());         
}

OR

 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);      
 }

Hopefully it resolves your issues.

The way through which i got the output is as follows:

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);

Thanks for the help @AliAzam :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM