Reputation: 5
I am practicing in "homedepot.com" right now, but the add to cart window makes me stuck. What I want to do right now is just click the close button on that add to cart window. Here is my code:
public static void main(String[] args) {
String path = "C://Webdrivers//geckodriver.exe/";
String url = "http://homedepot.com";
System.setProperty("webdriver.gecko.driver", path);
WebDriver driver = new FirefoxDriver();
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if(driver.findElement(By.xpath("//*[@id=\"container\"]/div[1]/div[2]/div/div[2]/div[1]/div/div[1]/a")).isDisplayed() )
{
System.out.println("Logo displayed.");
}
else
{
System.out.println("Logo not displayed.");
}
if(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div[2]")).isDisplayed() )
{
System.out.println("searchbar displayed.");
}
else
{
System.out.println("searchbar not displayed.");
}
if(driver.findElement(By.xpath("//*[@id=\"headerSearchGhost\"]")).isDisplayed() )
{
System.out.println("'What can we help you find today?' displayed.");
}
else
{
System.out.println("'What can we help you find today?' not displayed.");
}
driver.findElement(By.xpath("//*[@id=\"headerSearch\"]")).sendKeys("hammer");
driver.findElement(By.xpath("//*[@id=\"headerSearchButton\"]")).click();
if(driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[1]/div[5]/div[2]/div[2]/div[1]/div[1]/div/div/div[1]/div/div[3]/div[1]/a")).isDisplayed() )
{
System.out.println("'Husky 16 oz. Fiberglass Claw Hammer' displayed.");
}
else
{
System.out.println("'Husky 16 oz. Fiberglass Claw Hammer' not displayed.");
}
driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[1]/div[5]/div[2]/div[2]/div[1]/div[1]/div/div/div[1]/div/div[4]/div[3]/div/a/span")).click();
// driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// driver.findElement(By.className("thd-overlay__close")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.className("thd-overlay__close"))).click();
I practiced automationparcitce.com before too, that add to cart for me had the same problem. My friend used: // driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // driver.findElement(By.className("thd-overlay__close")).click();
this works for her, so i don't know what happen to mine?
Upvotes: 0
Views: 485
Reputation: 1275
Please use this code to do so:
I have gone manually to the add to cart button and then executed the steps to open and close the "Add to Cart" popup :
package com.demo.core;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HomeDepoTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources\\chromedriver-2.35.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.navigate().to("https://www.homedepot.com/s/hammer?NCNI-5");
Scanner sc= new Scanner(System.in);
System.out.println("Holding Exceution until manually proceeding steps upto Add to Cart button");
System.out.println("Manually go to the add to cart button and then press any integer ....");
int i = sc.nextInt();
List<WebElement> addToCartButton = driver.findElements(By.xpath("//span[contains(.,'Add to Cart')]"));
addToCartButton.get(0).click(); // clicking on first "Add to Cart" button
WebElement addToCartPopup = driver.findElement(By.xpath("//iframe[@src and contains(@class,'thd-overlay-frame')]"));
driver.switchTo().frame(addToCartPopup);
WebElement closePopUpButton = driver.findElement(By.xpath("//a[@data-automation-id='headerDesktopCloseAddToCartOverlay']"));
closePopUpButton.click();
}
}
Hope this helps you.
Upvotes: 0
Reputation: 433
Before Clicking on Close Button, Insert below Code:
driver.switchTo().frame(driver.findElement(By.xpath("//div[@data-direction='bottom']/div[2]/div/iframe")));
Then Write:
driver.findElement(By.className("thd-overlay__close")).click();
Let's Check Whether it is working or not.
Upvotes: 0
Reputation: 33
You may have to tell selenium to switch frames to the popup, since it's an iframe. driver.switch_to.frame(By., 'name')
I believe is what is used in python, not sure about Java. Then you can tell it to click the button.
Upvotes: 0