-1

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:

//*[@class='btn__inner']

HTML Code: enter image description here

Code:

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;
        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("ss3777010@gmail.com");
        driver.findElement(By.id("j_password")).sendKeys("Soniya@123");
        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();
        //String a=driver.findElement(By.xpath("//div[@class='product-item__rating']/span")).getText();
        //System.out.println(a);
        List<WebElement> b=driver.findElements(By.xpath("//div[@class='product-item__rating']/span[contains(.,'4.5')]"));
        System.out.println(b.size());

for(WebElement a:b)
{

    System.out.println(a.getText());
    }
}
  • 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
0

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

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

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