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();
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:
You can find a detailed discussion in org.openqa.selenium.ElementNotVisibleException: Element is not currently visible while clicking a checkbox through SeleniumWebDriver and Java