最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

selenium webdriver - After writing a full xpath its still showing no such element found even its showing only 1 matching element

programmeradmin3浏览0评论

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.

Share Improve this question edited Feb 19 at 3:29 Krishnan Mahadevan 14.7k6 gold badges37 silver badges70 bronze badges asked Feb 14 at 2:32 AniketAniket 11 silver badge 2
  • 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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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.

发布评论

评论列表(0)

  1. 暂无评论