1

I am trying to automate one case on the following page:

http://automationpractice.com/index.php?id_product=4&controller=product

With step:

Load Page > Click 'Add to cart' > Popup appears with buttons > Press 'Proceed to checkout'

However, my code is failing on Click "elementButton.click()"

The Exception I get:

"Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible "

The element is enabled; however, it is invisible either because it's a popup and I have to move to popup window and then click check out, or Alert/Pop seems to not working.

If anyone please could assist. Many thanks in advance.


driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[text()='Add to cart']")).click();

        WebElement elementButton = driver.findElement(By.xpath(".//a[contains(@title,'Proceed to checkout')]"));

        System.out.println(" ****elementButton**********" + elementButton.isEnabled());  // returning true
        elementButton.click();

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
Fuji
  • 15
  • 1
  • 7

2 Answers2

1

Refer to the error you get: ....element not visible, after Add to cart may you need wait until the element visible.

You can use visibilityOfElementLocated.

Following import:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

Try the bellow code:

driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[text()='Add to cart']")).click();

WebElement elementButton = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//a[contains(@title,'Proceed to checkout')]")));
elementButton.click();
frianH
  • 7,295
  • 6
  • 20
  • 45
  • Thank you, Could you please guide me why implicitlyWait doesn't work here. Isn't it implicitlyWait suppose to wait for a certain time and meanwhile if the element appears it should click if no then throw an exception Please clarify this for me – Fuji Sep 20 '19 at 11:34
1

To Load Page > Click 'Add to cart' > Popup appears with buttons > Press 'Proceed to checkout' you need to to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • cssSelector:

    driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("p#add_to_cart>button span"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[title='Proceed to checkout']>span"))).click();
    
  • xpath:

    driver.get("http://automationpractice.com/index.php?id_product=4&controller=product");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Add to cart']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Proceed to checkout']/span"))).click();
    
  • Browser Snapshot:

automationpractice

You can find a detailed discussion in org.openqa.selenium.ElementNotVisibleException: Element is not currently visible while clicking a checkbox through SeleniumWebDriver and Java

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352