0

I am trying to automate a purchase on a e-commerce website (used only for training) in Selenium Webdriver using Java. When I click on Add To Cart, a popup window appears with "Continue Shopping" or "Proceed to Checkout" buttons. The problem is, Selenium is unable to detect the Checkout button. On further investigation (isDisplayed()), I found out that it is unable to detect the entire popup window.All I get is ElementNotVisibleException.

I tried below options: 1) Checked if there are multiple windowHandles and found out there is only one window handle. 2) Checked if the popup is another frame. But it is part of the main frame. So switching to another frame is also ruled out. 3) I tried scrolling down the window a little. 4) Tried using WebdriverWait to locate presence of Element.

I am not sure what I am missing here. Any help is appreciated.

Below is the exact page where I am having trouble. http://automationpractice.com/index.php?id_product=4&controller=product

Snippet of my code:

WebElement proceed_to_checkout = (new WebDriverWait(driver, 20)).until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='layer_cart']/div[1]/div[2]/div[4]/a/span"))) ;
proceed_to_checkout.click();
  • Hi , could you please upload a screenshot of the popup? – Shah Dec 23 '15 at 19:57
  • Hi Shah, thanks for your response. I have provided the link for the training website. Just clicking on "Add to Cart" will open the popup. Anyway, here is the screenshot: https://www.dropbox.com/s/j5jpk2s4i832g7q/Capture.JPG?dl=0 – Pavan Kulkarni Dec 24 '15 at 08:20

2 Answers2

0

after looking at the popup, I tried to handle the situation like this:

  WebDriver wd = new FirefoxDriver();
        wd.get("http://automationpractice.com/index.php?id_product=4&controller=product");
        Thread.sleep(2000L);
        wd.findElement(By.xpath(".//*[@id='add_to_cart']/button")).click();
        Thread.sleep(2000L);
        wd.findElement(By.xpath(".//*[@id='layer_cart']/div[1]/div[2]/div[4]/a/span")).click();

I am able to click the button using this code. Let me know if this works for you or you need explanation but I think the code is quite straightforward.

Shah
  • 413
  • 2
  • 11
  • Sorry for the late reply! This works. I think it was a timeout issue. But I believe the solution provided by Mrunal is better as it uses implicit timeout. Thanks a lot! – Pavan Kulkarni Dec 28 '15 at 08:42
0

Well this works very well for me without any hiccup:

package queries;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SQ34442686 {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver=new FirefoxDriver();
        driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[text()='Add to cart']")).click();;
        driver.findElement(By.xpath(".//a[contains(@title,'Proceed to checkout')]")).click();
        driver.close();
    }

}
Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71
  • Sorry for the late reply! This works. I think it was a timeout issue. Thanks a lot! – Pavan Kulkarni Dec 28 '15 at 08:43
  • Can you tell me why the explicit wait in my code did not work here? Thanks. – Pavan Kulkarni Dec 28 '15 at 09:25
  • Here you can find the difference between implicit and explicit wait [link](http://stackoverflow.com/questions/10404160/when-to-use-explicit-wait-vs-implicit-wait-in-selenium-webdriver) – Shah Dec 28 '15 at 09:51
  • One thing as a side note..never use absolute XPATHs always try to keep them dynamic and that is possible root cause for your issue – Mrunal Gosar Dec 30 '15 at 13:44