我现在正在“ homedepot.com”中练习,但是添加到购物车窗口使我陷于困境。我现在想做的就是单击添加到购物车窗口中的关闭按钮。这是我的代码:
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();
这对她有用,所以我不知道会发生什么?
答案 0 :(得分:0)
由于它是iframe,因此您可能必须告诉selenium将帧切换到弹出窗口。 driver.switch_to.frame(By., 'name')
我相信python中使用的是什么,不确定Java。然后,您可以告诉它单击按钮。
答案 1 :(得分:0)
在点击关闭按钮之前,在代码下方插入:
driver.switchTo().frame(driver.findElement(By.xpath("//div[@data-direction='bottom']/div[2]/div/iframe")));
然后写:
driver.findElement(By.className("thd-overlay__close")).click();
让我们检查其是否正常工作。
答案 2 :(得分:0)
请使用以下代码进行操作:
我已经手动转到“添加到购物车”按钮,然后执行打开和关闭“添加到购物车”弹出窗口的步骤:
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();
}
}
希望这对您有所帮助。