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:


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

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;
        driver = new ChromeDriver();

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

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

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

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

Solution :

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


Here also you can use text() as function ,


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

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:


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.