I have customized an XPath for adding a product which has discount 30% for 2. Now I am facing problem while adding this into the cart.

package sample;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

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

public class Coop_angebote {

    public static void main(String[]args) throws InterruptedException{

    ChromeOptions options = new ChromeOptions();
     Map<String, Object> prefs = new HashMap<String, Object>();
      prefs.put("credentials_enable_service", false);
      prefs.put("password_manager_enabled", false); 
      options.setExperimentalOption("prefs", prefs);
    WebDriver driver = new ChromeDriver(options);
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

    // For Login Functionality

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("window.scrollBy(0,800)", "");


    JavascriptExecutor jse1=(JavascriptExecutor)driver;
    jse1.executeScript("window.scrollBy(0,400)", "");


    JavascriptExecutor jse2=(JavascriptExecutor)driver;
    jse2.executeScript("window.scrollBy(0,800)", "");


    JavascriptExecutor jse3=(JavascriptExecutor)driver;
    jse3.executeScript("window.scrollBy(0,800)", "");


    JavascriptExecutor jse4=(JavascriptExecutor)driver;
    jse4.executeScript("window.scrollBy(0,800)", "");


    JavascriptExecutor jse5=(JavascriptExecutor)driver;
    jse5.executeScript("window.scrollBy(0,250)", "");


    JavascriptExecutor jse6=(JavascriptExecutor)driver;
    jse6.executeScript("window.scrollBy(0,250)", "");

  //driver.findElement(By.xpath("//*[contains(text(),'30% ab 2')]")).click();

    List<WebElement> var=driver.findElements(By.xpath("//*[contains(text(),'30% ab 2')]"));

    for(int i = 0;i<=var.size();i++){

         List<String> li = new ArrayList<String>();
         System.out.println("ACTUAL:'"+ var.get(i).getText().trim() + "'");
           if(var.get(i).getText().trim().equals("30% ab 2"))
               System.out.println("IN iF:");

I have attached screenshot for HTML code.

  • 1
    Which error did you get??
    – Bharat Mane
    Jul 14, 2017 at 4:54
  • I didnt get any error in console. but the required action of adding products into the cart is not completed and the page is navigated to some other instead of adding products into the cart. Hello Lauda, //div[@class='product-item'][.//span[contains(.,'30%')]]//button[@data-add-to-basket] This x path worked for me. thank you for your help.Now i can add all products into the cart which is in category 30%for2. Jul 14, 2017 at 5:00

2 Answers 2


Same as for the other one, the xpath for add to cart button should be:



The mistake you're making is here:

//the below is ok
List<WebElement> var = driver.findElements(
        By.xpath("//*[contains(text(),'30% ab 2')]"));

//use a for each loop to make your life
//easier than keeping track of .size and i's
for (int i = 0; i <= var.size(); i++) {
    //not used
    List<String> li = new ArrayList<String>();
    //not assigned to anything
    System.out.println("ACTUAL:'" + var.get(i).getText().trim() + "'");

    if (var.get(i).getText().trim().equals("30% ab 2")) {
        //The above is your mistake
        System.out.println("IN iF:");

You'll need to click the "btn_inner" button element descending ancestor of var.get(i) in the approach you've given, using driver.findelement at that point will just returns the first visible "btn_inner" element in the whole page, the driver doesn't hold memory to where the last element(s) you called to find were. It re-evaluates the whole present page every single time it's called.

As I think it's going to be a bit hard for you to do the above proposal, you can also try to directly locate the buttons related to specified discount products based on an xpath like this:

findElements(By.xpath("//*[contains(text(),'30% ab 2')]/*[@class='btn__inner']")

which means to return the direct-child button element of all elements that contain '30% ab 2' text. Then by using something similar to your original approach, you can just re-iterate over a list of the above found elements and just .click() them to add to cart as you intended to do. P.S. try to learn how to use css selectors as well; it's a much more robust way to create tests and future-proof in that it doesn't care for the exact layout of the page as long as the element you want is still visible. With your approach even a single button being added/removed and you'll have to change up all your By's.

  • Please, take some time and properly format your code.
    – beatngu13
    Jul 14, 2017 at 9:12
  • @beatngu13 My only code contribution is the couple lines on the end, the first code is the OPs.I only slightly un-formatted then so my added comments are visible without scrolling, dont think it should be that much of a nuisance..
    – Leon
    Jul 14, 2017 at 9:15
  • 1
    Personally, I think following the boy scout rule is a good way to improve the quality of this site.
    – beatngu13
    Jul 14, 2017 at 9:20
  • Absolutely, but I favored comments being conveniently visible instead of formatted code with comments outside reach, since its only a few relevant lines of code I copied from OP, not the whole thing, so hopefully shouldn't be too hard to follow.
    – Leon
    Jul 14, 2017 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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