简体   繁体   中英

Selenium :Unable to click the same Close button on overlay when 2nd product is added to cart.For 1st item,able to click Close button perfectly

//Main Class TestNg class
 public class XCartCheckout extends TestBase {
  public static WebDriver driver;
  StoreHomePage ohome;

  // ArrayList<String> productname = new ArrayList<String>();
  @Test(dataProvider = "DataFromExcel", priority = 1)
  // public void Ad`enter code here`dToCartTest(productname) throws InterruptedException {
  public void AddToCartTest(String[][] productname) throws InterruptedException {
   int b = 0;
   ArrayList < String > arrlist = new ArrayList < String > ();
   boolean t = true;
   System.out.println("length =" + productname.length);
   System.out.println("arraylist values are");
   for (int i = 0; i < productname.length; i++) {
    for (int j = 0; j < productname[i].length; j++) {
     // System.out.println("productname=" + productname[i][j]);
     arrlist.add(productname[i][j]);
     // System.out.println("b=" + b+ "productname="+productname[i][j]);
     // b++;
    }

   }

   for (int k = 0; k < arrlist.size(); k++) {
    System.out.println(arrlist.get(k));
   }

   driver.get("");
   driver.manage().window().maximize();
   ohome = new StoreHomePage(driver);
   ohome.movingBanners();
   boolean sortstatus = ohome.selectSortOrder();
   b = Boolean.compare(sortstatus, t);

   if (sortstatus == t) {
    for (int l = 0; l < arrlist.size(); l++) {
     String cartmsg = ohome.addToCart(arrlist.get(l));
     Assert.assertEquals(cartmsg, "You have just added");
     System.out.println("Product added to cart");
    }
   }

  }
  //Add to Cart Method in my subclass:
  public String addToCart(String prodnamefrmexcel) {
   String successmsg = null;
   i++;
   try {

    WebElement prodname = driver.findElement(By.xpath("//h5/a[contains(text(),'" + prodnamefrmexcel + "')]"));
    WebElement addtocartbtn = driver.findElement(By.xpath("//h5/a[contains(text(),'" + prodnamefrmexcel +
     " ')]//parent::h5//following-sibling::div//span[text()='Add to cart']"));
    Actions action = new Actions(driver);
    Action action1 = action.moveToElement(prodname).moveToElement(addtocartbtn).build();
    action1.perform();
    addtocartbtn.click();
    Thread.sleep(4000);
    successmsg = driver.findElement(By.xpath("(//span[text()='You have just added'])['" + i + "']"))
     .getAttribute("innerText");
    System.out.println(successmsg);

    Thread.sleep(3000);

    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//span[text()='You have just added'])['" + i + "']//following-sibling::button"))).click();


   } catch (Exception e) {
    e.printStackTrace();
   }
   return successmsg;
  }
 }

I am trying to add 2 items to cart.While adding each item,an overlay with close button is displayed.I am trying to click Close button after adding each item.After adding 1st item,I am able to click Close button.But after adding 2nd item,unable to click close button. The following is the error received:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: (//span[text()='You have just added'])['2']//following-sibling::button (tried for 30 second(s) with 500 milliseconds interval)

Screenshots containing the webpage and html elements are attached.

web-page-1

web-page-2

Try to click with JsExecutor:

element = driver.findElement(By.xpath("your_xpath"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM