java - 无法检查元素的状态
问题描述
我第一次尝试自动化电子商务应用程序的完整流程。单击“添加到购物车”按钮后,将显示数量微调器而不是该按钮,并带有“+”和“-”按钮。我正在尝试确定是否单击了按钮。如果是这样,请单击“-”按钮,然后再次单击该元素的添加到购物车按钮,然后移至下一个按钮,依此类推。这就是我这样做的目的,但没有任何运气:
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>
解决方案
要单击减少按钮,您可以尝试以下代码:
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 ")))
推荐阅读
- postgresql - 当复制落后于备用时,Pgpool 在备用节点而不是主节点上执行查询
- python - 将字符串更改为字典中的整数。Python
- php - 正则表达式和重写规则
- python - 想着特质。动态更新 GUI 中的列表
- json - Acumatica - 创建销售订单时出错 - 变体/子项不能为空 - REST API
- javascript - 如何在 .Net Core 中压缩 Json Action Result 对象?
- python - 编写条件 For 循环对列表的子集求和并返回二进制值
- reactjs - react ssr 和代码拆分的优点和缺点,现在是 React.Lazy
- python - 将数据库对象传递给子类 QSqlTableModel
- android - 如何在 ViewPager 上设置非对称 pageMargin?