繁体   English   中英

Selenium:将第二个产品添加到购物车时,无法在叠加层上单击相同的关闭按钮。对于第一个项目,可以完美地单击关闭按钮

[英]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;
  }
 }

我正在尝试将 2 个项目添加到购物车。在添加每个项目时,会显示一个带有关闭按钮的叠加层。我试图在添加每个项目后单击“关闭”按钮。添加第一个项目后,我可以单击“关闭”按钮。但之后添加第二项,无法单击关闭按钮。 以下是收到的错误:

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)

附上包含网页和 html 元素的屏幕截图。

网页-1

网页 2

尝试用 JsExecutor 点击:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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