【发布时间】:2016-10-21 06:24:26
【问题描述】:
场景是,在我填写完表单的所有必要信息(假设它们都是有效的)然后选择保存后,我收到一条消息弹出大约 6-7 秒,说表单已成功保存然后它会随着它的 HTML 代码一起消失,这是
<div data-notify="container" class="alert-success animated" role="alert" data-notify-position="top-right">
<span data-notify="title">
<span class="glyphicon glyphicon-ok">
::before
</span>
<span> Saving</span>
<span data-modify="message">Successfully saved</span>
</span>
</div>
我正在尝试使用文本“成功保存”进行断言,代码如下:
String message = driver.findElement(By.cssSelector("span.glyphicon glyphicon-ok")).getText();
assertTrue(message.contains("Successfully saved"));
不幸的是,它返回失败,因为找不到元素
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span.glyphicon glyphicon-ok"}
我想知道是我的代码不正确还是消息消失的速度比驱动程序找到元素的速度快。
任何帮助或更正将不胜感激。
干杯, 丹尼
【问题讨论】:
-
这里的部分问题可能是您的 cssSelector 中不应该有空格。我建议只使用“span.glyphicon-ok”就足够了。正如其他一些人所提到的,您还需要使用 WebDriverWait。
-
为什么不把事情简化呢?只需获取页面源并验证您的消息是否存在?比如,driver.getPageSource().contains("保存成功");
-
嗨,Arpan,我确实在一开始就尝试过getPageSource,但没有成功,这就是我来这里寻求帮助的原因。 :-)
标签: javascript java selenium selenium-webdriver