当前位置: 首页 > 知识库问答 >
问题:

我总是搞不懂怎么在“添加到购物车”弹出窗口上点击btn

解河
2023-03-14

我现在正在“homedepot.com”练习,但是添加到购物车窗口让我卡住了。我现在要做的就是单击add to cart窗口上的close按钮。下面是我的代码:

 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();

我以前也练习过AutomationParcitce.com,为我添加到购物车也有同样的问题。我的朋友使用了://driver.manage().timeouts().implicitlywait(5,timeUnit.seconds);//Driver.FindElement(By.ClassName(“THD-Overlay__Close”)).Click();

这对她有用,所以我不知道我的怎么办?

共有1个答案

李鸿
2023-03-14

请使用以下代码执行此操作:
我已手动转到“添加到购物车”按钮,然后执行打开和关闭“添加到购物车”弹出窗口的步骤:

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();
    }

}

希望这对你有帮助。

 类似资料:
  • 我正在尝试做一个教程,学习python中的Selenium,但是我似乎无法让Selenium使用find_element_by_class或find_element_by_xpath单击“Add to Cart”按钮。问题是检查项目是否缺货,如果缺货则刷新网页并重新启动脚本。如果物料有库存,则应单击“添加到购物车”。 我正在使用: Python v3.9 Chrome v87

  • 最初,我选择了一个包(Woo订阅) 然后添加了所有详细信息。 但未提交。 回到网站,所以再次购买我需要选择一个包。所以我选择了这个包,并填写了详细信息,然后转到付款包。 现在在我的购物车中,两个包都存在(即我没有第一次购买就选择的包和最近的包) 如何修复此问题,使最新选定的一个在购物车中,而较早的一个在选定最新的一个后立即删除。 我尝试了这个Woocommerce从购物车中删除所有产品,并将当前产

  • 在商品分类页面上,当有人点击“添加到购物车”时,WooCommerce通过Ajax在此按钮下方添加“查看购物车”。我发现处理这个的脚本是 /assets/js/frontend/add-to-cart.js 现在,我想添加"Procceed to check out",这样某人就可以立即去结账。 这是脚本的输出: 有没有人做过类似的事情?

  • 问题内容: 单击后,我想重新标记“ 添加到购物车” 按钮,然后将一项添加到购物车, 再添加到购物车中 。 这可能吗? 我有第二个 转到购物车* 按钮的Child-Theme function.php ,并且它正在工作。 * 但是我不知道在将一件商品添加到购物车后如何解决此重新标签问题(商店仅出售一件尺寸不同的商品)。我希望我很清楚。 这是我的代码: 问题答案: 更新: 在下面,您将找到使此重新标记

  • 我一直在研究一种使用Selenium在网站上单击add to cart按钮的方法,但遇到了麻烦,页面打开并将我带到我需要的页面,但没有单击该按钮。我已经尝试了所有的find_element_,但似乎都不起作用,我现在的代码是: 按钮元素的HTML代码 谢谢你!

  • 我正在开发一个电子商务网站使用wordpress和cilo wordpress主题。 该网站是所有一起在桌面和其他媒体,但当我的屏幕大小小于769px,添加到购物车按钮是不工作的signle产品页面。点击这里查看一个示例帖子 该主题在演示中运行良好 此主题在主题文件夹中使用woocommerce 在我的站点上尝试了其他主题,其中add to cart在移动站点上起作用 我尝试升级了所有内容,包括主