0

我被困在 Amazon.com 的自动化中

自动化步骤:

  1. 打开 www.amazon.com 网站。
  2. 在搜索框中输入文本“<strong>耳机”。点击进入
  3. 从第 1 页显示的结果中,将所有标记为“<strong>Best Seller”的商品添加到购物车。 

我试过的代码:

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\****\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver =  new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://www.amazon.com");
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.id("twotabsearchtextbox")));
    searchBox.click();
    searchBox.sendKeys("Headphones"+Keys.ENTER);
    Actions action = new Actions(driver);
    List<WebElement> bestSellers = driver.findElements(By.xpath("//span[text()='Best Seller']/ancestor::div[@class='sg-row']/following-sibling::div[@class='sg-row']/child::div[1]"));
    for(int i=1;i<=bestSellers.size();i++) {
        action.moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Best Seller']/ancestor::div[@class='sg-row']/following-sibling::div[@class='sg-row']/child::div['"+i+"']")))).build().perform();
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Best Seller']/ancestor::div[@class='sg-row']/following-sibling::div[@class='sg-row']/child::div['"+i+"']"))).click();
        wait.until(ExpectedConditions.elementToBeClickable(By.id("add-to-cart-button"))).click(); 
        //System.err.println(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[contains(text(),'Added to Cart')]"))).getText());
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.uss-o-close-icon.uss-o-close-icon-medium"))).click();
        driver.navigate().back();
        driver.navigate().refresh();
        System.err.println("try to find next best seller item ");
    }

}

它正在为所有迭代添加第一个畅销商品。但我想将所有 4 个最畅销的产品添加到购物车。任何帮助将不胜感激。

4

2 回答 2

1

在下面的代码中,用于获取所有没有赞助(重复)的畅销商品。使用来自畅销元素的流获取 href 属性。迭代畅销书导航到 url,添加到购物车并等待成功消息:

import org.openqa.selenium.support.ui.ExpectedConditions;

//...

List<WebElement> bestSellers = driver.findElements(
        By.xpath("//span[text()='Best Seller']" +
                "/ancestor::div[@data-asin and not(.//span[.='Sponsored'])][1]" +
                "//span[@data-component-type='s-product-image']//a"));
List<String> bestSellersHrefs = bestSellers.stream()
        .map(element -> element.getAttribute("href")).collect(Collectors.toList());

bestSellersHrefs.forEach(href -> {
    driver.get(href);
    wait.until(elementToBeClickable(By.id("add-to-cart-button"))).click();
    boolean success = wait.until(or(
            visibilityOfElementLocated(By.className("success-message")),
            visibilityOfElementLocated(By.xpath("//div[@id='attachDisplayAddBaseAlert']//h4[normalize-space(.)='Added to Cart']")),
            visibilityOfElementLocated(By.xpath("//h1[normalize-space(.)='Added to Cart']"))
    ));
});
于 2019-08-13T09:56:15.433 回答
1

看起来你错误的位置增加计数i,你可以试试这个:

action.moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//span[text()='Best Seller']/ancestor::div[@class='sg-row']/following-sibling::div[@class='sg-row']/child::div[1])[" +i +"]")))).build().perform();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//span[text()='Best Seller']/ancestor::div[@class='sg-row']/following-sibling::div[@class='sg-row']/child::div[1])[" +i +"]"))).click();

并关闭按钮,您可以使用此定位器:

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(@class,'uss-o-close-icon uss-o-close-icon-medium') or contains(@class,'a-link-normal close-button')]"))).click();

我看每个关闭按钮都没有相同的定位器,这里也有它的挑战。

于 2019-08-13T09:48:10.223 回答