【发布时间】:2019-08-04 09:46:21
【问题描述】:
我正在自动化 selenium/Java 中的网络商店。如果用户尚未选择产品的尺寸,则会出现一条消息,指出尺寸旁边的“这是必填字段”。我正在尝试编写一个“if”语句来断言此消息是否存在以及如果存在则要采取的操作,但我无法让它工作。大概是这样的:
WebElement sizeIsRequiredMsg = driver.findElement(By.cssSelector("#advice-required-entry-select_30765)"));
WebElement sizeSmallButton = driver.findElement(By.cssSelector("#product_info_add > div.add-to-cart > div > button"))
if (sizeIsRequiredMsg.equals("This is a required field.")) {
action.moveToElement(sizeSmallButton);
action.click();
action.perform();
}
我尝试了几种不同的变体,将“这是必填字段”消息用作 Web 元素。不确定我是否需要以某种方式将消息的 WebElement 转换为字符串?或者包括一个布尔值?有人可以帮忙吗?
【问题讨论】:
-
sizeIsRequiredMsg 变量的返回类型是 WebElement,因此您无法将 WebElement 与 String 进行比较,因此您必须使用 sizeIsRequiredMsg.getText() 方法来获取文本,然后才能比较两个字符串。
标签: java string selenium if-statement automation