首页 > 解决方案 > 无法检查元素的状态

问题描述

我第一次尝试自动化电子商务应用程序的完整流程。单击“添加到购物车”按钮后,将显示数量微调器而不是该按钮,并带有“+”和“-”按钮。我正在尝试确定是否单击了按钮。如果是这样,请单击“-”按钮,然后再次单击该元素的添加到购物车按钮,然后移至下一个按钮,依此类推。这就是我这样做的目的,但没有任何运气:

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 &amp; 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 &amp; Vegetables"></button>
      </div>

标签: javaseleniumselenium-webdriver

解决方案


要单击减少按钮,您可以尝试以下代码:

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 ")))

推荐阅读