我正在尝试第一次自动化电子商务应用程序的完整流程。单击“添加到购物车”按钮后,将显示数量微调器,而不是带有“ +”和“-”按钮的那个按钮。我试图确定是否单击了按钮。如果是这样,请单击“-”按钮,然后再次单击该元素的“添加到购物车”按钮,然后移至下一个按钮,依此类推。这就是我这样做的目的,但是没有任何运气:
private static void addProductsToCart()
{
List<WebElement> buttons = driver.findElements(By.className("actions__addToCart"));
WebElement action = driver.findElement(By.xpath("//*[@id=\"content\"]/div/section/ul/div/div/li/div/div/div/div/div/button[1]"));
for(int i = 0; i <= buttons.size(); i++)
{
if(action != null && action.isDisplayed())
{
action.click();
}
buttons.get(i).click();
if(i == 4)
{
break;
}
}
}
任何帮助将不胜感激。
<div class="quantitySpinner alignmentContainer__element" data-bind="visible: size().quantity() > 0" style="">
<button class="decrease" data-bind="click: decrementQuantity, attr: {'aria-label': ogs.gsa.resources.Accessibility_DecreaseQuantity.formatWith({productBrand: model.Brand, productName: model.Name})}" aria-label="Subtract One Birds Eye Steamfresh Chef's Favorites Rotini & Vegetables"></button>
<label data-bind="text: size().quantity, attr:{'aria-live':'polite', 'aria-atomic':'true'} " aria-live="polite" aria-atomic="true">1</label>
<button class="increase" data-bind="click: incrementQuantity, attr: {'aria-label': ogs.gsa.resources.Accessibility_IncreaseQuantity.formatWith({productBrand: model.Brand, productName: model.Name})}" aria-label="Add One Birds Eye Steamfresh Chef's Favorites Rotini & Vegetables"></button>
</div>
答案 0 :(得分:0)
要单击减少按钮,可以尝试以下代码:
WebDriverWait wait = new WebDriverWait(driver , 20);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.decrease"))).click();
如前所述,您想单击click again on Add to Cart button
为此:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath of add to cart button
or any other locator as per your convenience ")))