gpt4 book ai didi

java - 如何添加 4 个畅销耳机商品以添加到 Amazon.com 的购物车

转载 作者:行者123 更新时间:2023-12-02 01:22:18 25 4
gpt4 key购买 nike

我陷入了 Amazon.com 的自动化

自动化步骤:

  1. 打开www.amazon.com网站。
  2. 在搜索框中输入文本“耳机”。按回车键
  3. 从第 1 页显示的结果中,将所有标记为“畅销商品”的商品添加到购物车。

代码我已经尝试过:

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 个最畅销产品添加到购物车。任何帮助将不胜感激。

最佳答案

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

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']"))
));
});

关于java - 如何添加 4 个畅销耳机商品以添加到 Amazon.com 的购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57472751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com