I always can't figure out how to click btn on add to cart popup window


I always can't figure out how to click btn on add to cart popup window



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?




3 Answers
3



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





yes yours works, but when i put your part to mine it sill shows: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@data-automation-id='headerDesktopCloseAddToCartOverlay']"}
– RaiDen Cheung
Jul 2 at 8:22





only the page is different, could you try this page? homedepot.com/s/hammer?NCNI-5
– RaiDen Cheung
Jul 2 at 8:52





I have updated the answer for alert and popup. It is working now. Your popup is taking time to load properly. You can add wait condition for the close button to appear or can use static wait here. Do not forget to accept and upvote the answer.
– dangi13
Jul 2 at 10:04






did it worked for you.
– dangi13
Jul 2 at 12:32





when you use only "thd-overlay-frame" class, there are hidden iframes present that also contains this class but not visible , when I use both "src" and this class and search for that element then it will always give the visible iframe that contains a src tag.
– dangi13
Jul 2 at 16:42




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.


driver.switch_to.frame(By., 'name')





How could I find the frame name or id?
– RaiDen Cheung
Jul 1 at 5:35





There are a couple of ways. You can declare the frame like iframe = driver.find_element_by_class_name('thd-overlay-frame') and then use WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it(iframe)). Or, what I have used and works also is by xpath and uses contains, if it's a dynamic frame that switches the name/id sometimes. iframe = driver.find_element_by_xpath("//*[contains(@class, 'frame')]"). Or, you can switch by index. driver.switch_to.frame(0) or (1). Depends on how many iframes there are on screen, but it always starts at 0. @RaiDen Cheung
– Grim
Jul 1 at 12:22



iframe = driver.find_element_by_class_name('thd-overlay-frame')


WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it(iframe))


contains


iframe = driver.find_element_by_xpath("//*[contains(@class, 'frame')]")


driver.switch_to.frame(0)


(1)


0





Oh and to find the frame, just inspect the popup. There should be an html tag that says <iframe> inside of a <div> of the popup. Home Depot's iframe class is thd-overlay-frame.
– Grim
Jul 1 at 12:24


thd-overlay-frame





I tried "thd-overlay-frame" a lot time before , but it didnt work!
– RaiDen Cheung
Jul 1 at 21:12





Did your friend use the same name for the overlay-frame? If she got it to work maybe you could see what she did differently? Maybe try using the xpath or index methods to find the frame. If it gives you an error for the button NoSuchElementException and not the iframe, it switched to the iframe correctly. Unfortunately, I'm having this same issue on a different script I'm writing. The button inside of the iframe seems to not "exist" according to Selenium.
– Grim
Jul 1 at 22:07


NoSuchElementException



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.





no it isnt working. it says:Unable to locate element: //div[@data-direction='bottom']/div[2]/div/iframe
– RaiDen Cheung
Jul 1 at 20:30






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages