我正在尝试通过网络驱动程序验证当我点击按钮"添加到购物车"时,它会切换到另一个按钮"从购物车中删除"。页面上的所有项目都有一个添加到购物车并从购物车按钮中移除,具有相同的代码(我相信这是什么真的让我失望)
<button> class="tg-button tg-button--large ng-binding tg-button--primary" ng-click="toggleDeviceToShoppingCart(deviceCatalogItem)" ng-class="{'tg-button--primary': !deviceIsInCart(deviceCatalogItem)}">Add to Cart</button>
<button> class="tg-button tg-button--large ng-binding" ng-click="toggleDeviceToShoppingCart(deviceCatalogItem)" ng-class="{'tg-button--primary': !deviceIsInCart(deviceCatalogItem)}">Remove from Cart</button>
有关如何验证按钮确实已更改的任何建议。
答案 0 :(得分:0)
不完全相同。
第一个解决方案:Add to Cart
按钮具有Remove from Cart
按钮没有的类属性 - tg-button--primary
WebElement button = driver.findElement(By.cssSelector(".tg-button.tg-button--large.ng-binding")); // get the button
String classAttributes = button.getAttribute("class"); // get the class attribute
if (classAttributes.contains("tg-button--primary")) {
// "Add to Cart" button
}
else {
// "Remove from Cart" button
}
第二种解决方案:按钮的文字不同
WebElement button = driver.findElement(By.cssSelector(".tg-button.tg-button--large.ng-binding")); // get the button
String text = button.getText(); // get the text
if (text.contains("Add to Cart")) {
// "Add to Cart" button
}
else {
// "Remove from Cart" button
}
答案 1 :(得分:0)
试一试:
findElement(by.cssSelector("Your selector here"))
.getAttribute("class")
.contains("tg-button--primary");
如果是,则为第一个按钮。如果是假,则是第二个。 我希望语法是正确的,我只使用C#
在Selenium中编程