我被亚马逊的自动化所困扰。com公司
自动化步骤:
我尝试过的代码:
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个最畅销的产品添加到购物车中。任何帮助都将不胜感激。
在下面的代码中,xpath用于获取所有没有赞助(重复)的畅销书项目。使用stream从畅销书元素中获取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']"))
));
});
似乎您的位置错误,增加了计数,您可以尝试以下操作:
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();
我看每个关闭按钮都没有相同的定位器,这里也有它的挑战。