I want to select all the products having rating 4.5, I could do that with the help received from StackExchange. Now I want to add the products with rating 4.5 to the cart. However, I am not able to customize the XPath. I tried the below XPath:


HTML Code: enter image description here


package Test_package;

import java.util.ArrayList;
import java.util.List;

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

public class rating {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        driver = new ChromeDriver();
        //String a=driver.findElement(By.xpath("//div[@class='product-item__rating']/span")).getText();
        List<WebElement> b=driver.findElements(By.xpath("//div[@class='product-item__rating']/span[contains(.,'4.5')]"));

for(WebElement a:b)

  • Can you give a bit more detail on what you are trying to accomplish? – ECiurleo Jul 14 '17 at 8:08
  • @ECiurleo I need to add all the products to cart with the rating 4.5 – Pratiksha Jul 14 '17 at 8:16
  • @Sagar007 could you please help me in this case? – Pratiksha Jul 14 '17 at 8:18
  • Ok, I think you need to clarify your question then. Try reframing it as "I have this list of multiple items and the code xxx and I would like to select all items with 4 stars. I have tried the below but I get y error/selects wrong thing" – ECiurleo Jul 14 '17 at 8:23
  • You have an answer there with the xpath for the button of the products with rating 4.5 – lauda Jul 14 '17 at 8:24

The xpath for the button of products with that rating should be something like:


| improve this answer | |
  • Thanks a lot.... This xpath does work, however it adds only 1 product with rating 4.5. I need to add all the products with 4.5 rating. Could you please suggest something for that. – Pratiksha Jul 14 '17 at 10:44
  • I could add all the products to cart thanks a lot – Pratiksha Jul 14 '17 at 11:18

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.