Issue encountered while using Selenium: Unable to successfully click the Close button on overlay when a second product is added to the cart. However, there is no problem clicking the Close button for the first item added

//Custom TestNG Class for XCart Checkout
 public class XCartCheckout extends TestBase {
  public static WebDriver driver;
  StoreHomePage home;

  // 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 counter = 0;
   ArrayList < String > productList = new ArrayList < String > ();
   boolean flag = 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("Product Name=" + productname[i][j]);
     productList.add(productname[i][j]);
     // System.out.println("counter=" + counter+ "product name="+productname[i][j]);
     // counter++;
    }

   }

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

   driver.get("");
   driver.manage().window().maximize();
   home = new StoreHomePage(driver);
   home.movingBanners();
   boolean status = home.selectSortOrder();
   counter = Boolean.compare(status, flag);

   if (status == flag) {
    for (int l = 0; l < productList.size(); l++) {
     String message = home.addToCart(productList.get(l));
     Assert.assertEquals(message, "You have just added");
     System.out.println("Product added to cart");
    }
   }

  }
  //Add to Cart Method in my subclass:
  public String addToCart(String productNameFromExcel) {
   String successMessage = null;
   int i = 0;
   try {

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

    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 successMessage;
  }
 }

I am attempting to add 2 items to the cart. When adding each item, an overlay with a close button is displayed. I am trying to click on the Close button after adding each item. After adding the first item, I am able to click the Close button. However, after adding the second item, I am unable to click the close button. The error received is as follows:

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 of the webpage and HTML elements are attached.

web-page-1

web-page-2

Answer №1

Use this script to click on an element using JsExecutor:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to deal with Chrome's notification for downloading multiple files on "www.xyz.com" in automation using Robot Framework and SeleniumLibrary

Currently I am facing a challenge in my automation testing process. I am trying to download 3 files with a single button click when utilizing Robot Framework with SeleniumLibrary and writing code in VSCode. Upon clicking the button, Chrome displays a popup ...

Is selecting an element in Chrome causing problems during selenium automation?

driver.findElement(By.xpath("//button")).click(); anticipated outcome: Button successfully clicked. current outcome: Error caused by: org.openqa.selenium.ElementNotVisibleException: Element must be displayed to click (WARNING: The server did not provide ...

Encountered an issue while attempting to assess Jackson deserialization for this specific

I am currently in the process of establishing a many-to-many relationship between movies and users. However, I encountered an error while attempting to save a movie: 2017-12-01 16:12:43.351 WARN 17328 --- [nio-8090-exec-5] .c.j.MappingJackson2HttpMessageC ...

Refresh WebPage automatically after a Servlet successfully uploads and processes an image

I have a webpage that includes an image and a button. When the button is clicked, it uploads the image by submitting a form to a file upload servlet. However, after successfully uploading the image, the servlet does not display it in the img tag. Here is ...

Unable to access SVG element in a loop and retrieve all the contents

My goal is to extract job titles from a job-site by scraping the information. I want to navigate to the subsequent pages, extract the data, and continue until no more pages are available. However, when attempting to click on the next page marked as an svg ...

Verify if the application is installed on the device through programmatic means

Some may argue that this question is duplicated, but I believe it's not. I have installed an app on the emulator and am currently running the following code to check if the app is available. If the app is indeed available, it returns true. See the co ...

The youngster's window shut down following the selection of a value, making it impossible to switch the attention back to the

When I click on a link in the parent window, a child window opens. After selecting a value in the child window, it automatically closes but the control remains in the closed window. How do I bring back the control to the parent window? Here is the code sn ...

Update the content within every <td> element using AJAX/JQUERY on a table created from JSP

I have a database filled with descriptions and corresponding ID numbers. The table displays them like this: index.jsp <table> <tr> <td>Name:</td> <td>Id:</td> </tr> <c:forEach items ...

Guide on pressing the "Next" button on the Twitter login page with Selenium and Python

Currently, I am working on developing a Twitter automation bot using Selenium. However, I have encountered an error when attempting to log in and click on the next button after entering the user ID. I have experimented with various methods like xpath, lin ...

Mastering the art of bypassing the Javascript "onclick" button to extract data from a web page using

I am attempting to scrape data from a website, specifically a table. While I was able to successfully scrape the table, I encountered an issue when trying to scrape other pages. Unfortunately, these pages do not have any href links. When attempting to cli ...

The script functions properly when in debug mode, but does not run when switching to run mode in the IE browser

I have attempted running the following code in both Firefox and IE. It seems to be working fine in Firefox, however, I am experiencing issues in IE. I am using IE v11, Selenium v2.45, and have also tried versions 2.46 and 2.44. I am also utilizing IE Dri ...

Encountering the error message "java: incompatible types: java.lang.Object cannot be converted to org.testng.ISuiteResult"

While utilizing the reportNg tool to generate result reports for my automation framework, I noticed that it lacks the ability to display the testcase descriptions in the results. This led me to explore creating a custom HTML result report using the IReport ...

Discover an Element in Selenium using C# based on its class and text content

I am brand new to the world of testing and have not received any formal training in automated tests, so please be patient with me if I make any mistakes. I will do my best to figure this out. Basically, I am attempting to confirm that a specific employee ...

Python (Selenium) - Guide on extracting and storing data from multiple pages into a single CSV file

I am facing a challenge with scraping data from a web page that contains a table spanning multiple pages. I am using Python with selenium for this task. The website in question is: The issue I am encountering is related to navigating through the pages of ...

Navigating through the Robot Framework: Extracting specific data from a cell in an xlsx document

As someone who is new to Python and the Robot framework, I am looking to extract specific cell data for automation purposes. I have tried using the `Library ExcelLibrary` and a command Read Cell Data By Coordinates {Path to my excel file}/test.xlsx ...

Discovering a web element: A guide

Couldn't locate a specific element on the webpage https://cloud.google.com/products/calculator/ Trying to find and interact with the 'Number of GPUs' element on the aforementioned site, then select '1' from the dropdown menu that ...

The functionality of "Expectedconditions.not" in Selenium Webdriver is not performing as anticipated

WebDriverWait wait = new WebDriverWait(driver, 60) WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]")); System.out.println("Test"); wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocat ...

Problem encountered when integrating Jenkins with Maven

Encountering a PluginResolutionException while running code through Jenkins. The code works fine in Eclipse, but throws an exception when executed in Jenkins. public class LoginPageTest extends TestBase { // Code snippet here } This is the content of t ...

I'm struggling with a Python Bot issue, as it seems the driver is not being acknowledged

I'm encountering an error with my code while trying to run a WhatsApp bot. Can anyone help me figure out why this error is occurring? Here is the error message that I am getting: Error: Traceback (most recent call last): File "C:\Users&bs ...

The Chromedriver Manager fails to download after being converted to a .exe file using PyInstaller

Hello everyone at Stackoverflow, I have been using Selenium and Python to automate a task successfully in myscript.py. In an attempt to convert it to myscript.exe, I used the following command with PyInstaller: PyInstaller --onefile --windowed --icon=&quo ...