[将产品添加到购物篮时如何使用selenium -java显式等待购物篮的计数从零变为1

问题描述 投票:1回答:1

我试图将商品自动添加到亚马逊的购物篮中,并声称一旦将商品添加到购物篮中,便要检查购物篮的数量是否已更改。

所以我明确地等待添加产品,但是购物篮图标已经在页面中,因此我的代码无法在添加产品后获取购物篮的数量。

WebDriverWait waitDriver =新的WebDriverWait(driver,30);waitDriver.until(ExpectedConditions.visibilityOfElementLocated(By.id(“ nav-cart-count”)));

selenium-webdriver webdriverwait
1个回答
0
投票

如果仅涉及无项目->一些项目,您可以尝试等待nav-cart-1类,因为它仅在购物篮中有一些项目时出现。

WebDriverWait waitDriver = new WebDriverWait(driver,30); 
waitDriver.until(ExpectedConditions.visibilityOfElementLocated(By.css("span#nav-cart-count.nav-cart-1")));

但是如果您对精确计数感兴趣,这将不起作用。

如果要检查精确计数,则应在text()条件下使用xpath

    waitDriver.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text() = '1' and @id='nav-cart-count']")));
© www.soinside.com 2019 - 2024. All rights reserved.