簡體   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