HTML如下:
<section class="my-account-box-element">
<span class="item icon-home icon-color-1"></span>
</section>
需要根据根据条件从“icon-home icon-color-1”变为“icon-home icon-color-0”的类名进行断言。
HTML如下:
<section class="my-account-box-element">
<span class="item icon-home icon-color-1"></span>
</section>
需要根据根据条件从“icon-home icon-color-1”变为“icon-home icon-color-0”的类名进行断言。
First i must warn that you can't add two classes when searching for it using the By
, since 'item', 'item-home' on your example are the same. I'm going to assume the main difference is in 'item-color-1' and 'item-color-0'.
WebElement myAccountBoxElement = driver.findElement(By.className("my-account-box-element"));
WebElement spanItem = myAccountBoxElement.findElement(By.tagName("span"));
boolean itemColor = (spanItem.getAttribute("class").contains("item-color-1")) ? true : false;
if (itemColor) {
// do stuff for item-color-1 element
}
// do stuff for the item-color-0 element
}
The above code should work flawlessly provided the above code is the actual HTML, if there are more tags, use findElements()
instead and loop in it.
Also i went with a ternary if since it keeps a cleaner code, providing you are only working with those two elements
也可以用这个
By.className("classname");
boolean isElementPresent = driver.findElement(By.className("classname"));
根据您的需要更改
assertTrue(driver.findElements( By.Xpath(".//span[contains(@class,'icon-color-1')]")).size() != 0)