问题描述
这是我想要完成的:
check if element "Error" span exists
then do something if it does ===
else check if element "el2" span exists
then do something ====
else
do something
这是我的代码
let done = false;
let errorOccured = false;
let isManualStep = false;
do {
try {
console.log('Checking span.label-red=Error...................')
let errorStepExists = $('span.label-red=Error').isExisting();
if (errorStepExists) {
console.log('Error encountered...................')
done = true;
}
} else {
console.log('Error not encountered...................')
let el2Exists = $('span>td.col-md-2=Waiting').isExisting();
if (mel2Exists) {
console.log('Encountered...................')
}
} catch (e) {
console.log(e);
}
}
while (!done);
如果元素不存在,它似乎不会转到 catch 块,并且它没有通过我的测试,而是出现超时错误。
错误:超过 20000 毫秒超时。测试“xxxxxx”中的执行时间太长。尝试减少运行时间或增加测试规范的超时时间 (https://webdriver.io/docs/timeouts.html)。
解决方法
不确定如何在编程语言绑定中实现目标的最佳方式,但是在 Java 中最好的方式是使用 WebDriver 接口的“findElements(By locator)”方法。
此方法返回一个列表。然后,您可以通过检查此列表的大小来检查您的 WebElement 是否存在。 (如果大小不为 0,则您的 WebElement 存在)。
希望这有助于您在语言绑定中采用类似的方法。