-2

I want to select all the products having rating 4.5 and add to the cart. However, I am not able to customize the XPath. I tried the below XPath:

//div[@class='product-item__rating']/span 

This highlights all the ratings.

HTML Code:

<div class="product-item__rating">
<span class="rating">
<span class="visuallyhidden">Durchschnittliche Bewertung: 4.5 von 5</span>
<span class="rating__star rating__star--full" aria-hidden="true"/>
<span class="rating__star rating__star--full" aria-hidden="true"/>
<span class="rating__star rating__star--full" aria-hidden="true"/>
<span class="rating__star rating__star--full" aria-hidden="true"/>
<span class="rating__star rating__star--half" aria-hidden="true"/>
</span>
</div>

Selenium Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class rating {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.coopathome.ch/de/");
        driver.findElement(By.xpath(".//*[@id='mastheadMyAccountLink']/span")).click();
        Thread.sleep(2000);
        driver.findElement(By.id("j_username")).sendKeys("username");
        driver.findElement(By.id("j_password")).sendKeys("password");
        driver.findElement(By.id("loginOverlaySubmitLoginFormButton")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='nav']/ul/li[1]/a/span")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='subnav--item-m_0001']/li[1]/ul[1]/li[1]/a")).click();
    }

}
  • @Sagar0007 The solution worked for me. But now i am not able to add the products to cart. Could you please suggest something. – Pratiksha Jul 13 '17 at 11:50
  • This should be a separate question with your HTML code that includes the Add to cart button. – Kate Paulk Jul 13 '17 at 11:56
  • Please accept the answer to the solution that worked for you – ECiurleo Jul 13 '17 at 12:04
  • Pratiksha as you know this is answer of question part. You can comment in my answer for more information or create new question and let us know. Please accept right answer. – Sagar007 Jul 13 '17 at 13:10
1

Checking the 4.5 substring inside the invisible rating text as others already suggested is a good and probably the simplest way to go.

As an alternative, you can count down how many full and half starts there are for a product:

// getting all products with rating 4.5
List<WebElement> products = driver.findElements(By.xpath("" +
        "//div[@class = 'product-item' and " +
              "count(.//span[contains(@class, 'rating__star--full')]) = 4 and " +
              "count(.//span[contains(@class, 'rating__star--half')]) = 1]"));

for ( WebElement product: products) {
    System.out.println(product.findElement(By.cssSelector(".product-item__name")).getText());
}

It prints:

Äpfel Jazz ca. 769 g
Äpfel Pink Lady ca. 1 kg
Äpfel Jazz 5-6Stk ca. 1 kg
Äpfel Braeburn ca. 1 kg
Birnen Kaiser Alex. ca. 1 kg
Äpfel Golden ca. 1 kg
Äpfel Gala ca. 1 kg
1

This will get you the basket button element for any item with rating 4.5 on any of the pages I looked at:

//span[contains(text(),'4.5 von 5')]/ancestor::div[@class='product-item__details']//button

To use this to add all the items to the basket add the following to your code after you've opened the last page:

//Find items with 4.5 rating and add them to the cart
List<WebElement> items = driver.findElements(By.xpath("//span[contains(text(),'4.5 von 5')]/ancestor::div[@class='product-item__details']//button"));

for (WebElement item: items) {
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", item);
    item.click();
}
0

Solution :

As you are customize the Xpath , Just add some more info like :

//div[@class='product-item__rating']/span[contains(.,'4.5')]

Here also you can use text() as function ,

//div[@class='product-item__rating']/span[contains(text(),'4.5')]

No need to change and extra parameter or function.

Now store this as List of WebElements as under :

List<WebElement> AllRating= driver.findElements(By.xpath("//div[@class='product-item__rating']/span[contains(.,'4.5')]"));

Note : This answer is given from your HTML code.

  • Can I ask why this answer is down voted ? – Sagar007 Jul 25 '17 at 4:29
0

You need to find all items that contains this rating and from there continue with the path to add to cart.

In short you need to find add to cart for items with this rating.

A possible value for xpath is:

//div[@class='product-item'][.//span[@class='rating']/span[contains(.,'4.5')]]//button

After you added the first item to the cart you will see that this will match also for quantity control button, so you need to add an extra constraint resulting in:

//div[@class='main-content']//div[@class='product-item'][.//span[@class='rating']/span[contains(text(), '4.5')]]//button[@data-add-to-basket]

Instead of [@data-add-to-basket] you can also use [contains(@class, 'basket')]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.