Here is the complete code
@Test(priority = 2)
public void addToCart() throws InterruptedException {
WebElement addToCartBtn = driver.findElement(By.xpath("/html/body/div[2]/main/section[2]/div/div[1]/div[2]/section[2]/div[1]/button"));
wait.until(ExpectedConditions.elementToBeClickable(addToCartBtn));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", addToCartBtn);
Thread.sleep(1000); // Allow time for the scroll to complete
// Click the button using JavaScript to ensure the click triggers the event
js.executeScript("arguments[0].click();", addToCartBtn);
WebElement modal = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/main/section[2]/div/div[1]/div[2]"))); // Replace with correct modal XPath
System.out.println("Modal is visible: " + modal.isDisplayed());
addToCartBtn.click();
Thread.sleep(5000);
}
after click on addToCart
button it should open a modal pop-up but its not opening and fails in addToCartBtn
click.
Here is the complete code
@Test(priority = 2)
public void addToCart() throws InterruptedException {
WebElement addToCartBtn = driver.findElement(By.xpath("/html/body/div[2]/main/section[2]/div/div[1]/div[2]/section[2]/div[1]/button"));
wait.until(ExpectedConditions.elementToBeClickable(addToCartBtn));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", addToCartBtn);
Thread.sleep(1000); // Allow time for the scroll to complete
// Click the button using JavaScript to ensure the click triggers the event
js.executeScript("arguments[0].click();", addToCartBtn);
WebElement modal = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/main/section[2]/div/div[1]/div[2]"))); // Replace with correct modal XPath
System.out.println("Modal is visible: " + modal.isDisplayed());
addToCartBtn.click();
Thread.sleep(5000);
}
after click on addToCart
button it should open a modal pop-up but its not opening and fails in addToCartBtn
click.
- 1 Give us the Page HTML or the URL to check the XPath. – S A Commented Feb 14 at 3:24
- here is the url swagnet.addnectarstudio, search for 8000 and go to the product detail page – Aniket Commented Feb 14 at 9:34
1 Answer
Reset to default 1The XPath is correct and it detects the add-to-cart button. Even though you have used an absolute XPath, It works. I prefer to use a relative XPath.
But there are two add-to-cart buttons and you are trying to click on the same button twice. The XPath selects the first button. Then you used JavascriptExecutor to scroll and click on the button. Clicking on that button shows a modal and your code prints that in the console.
Then you are trying to click on the button again. But the button is hidden under the modal and your code gives an element click intercepted
error.
I'm guessing that the second time you are trying to click on the add-to-cart button on the modal.
To click the add to cart button it doesn't require javascript executor.
Try the following code.
public void addToCart() throws InterruptedException {
WebElement addToCartBtn = driver.findElement(By.xpath("//button[@aria-label='Menu' and normalize-space(text())='Add to Cart']"));
addToCartBtn.click();
WebElement modal = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@x-show='isShippingMenuOpen']/following-sibling::aside")));
System.out.println("Modal is visible: " + modal.isDisplayed());
WebElement modalAddToCartButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='text-right']/button[normalize-space(text())='Add to Cart']")));
modalAddToCartButton.click();
}
This code will click on the add-to-cart button on the modal. But the site will give you an alert saying to select the imprint method. You have to fill up the form correctly to add the item to the cart.