Webdriver中selenium.isElementPresent的替代方法是什么 [英] What is the alternative of selenium.isElementPresent in Webdriver

查看:189
本文介绍了Webdriver中selenium.isElementPresent的替代方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查下拉字段是否具有给定值,但未选择这些值,因此其不会在下拉框中显示. 我有以下Xpath作为元素

Hi I need to check a drop down field is having given values but those values are not selected so its not getting displayed in the dropdown box. I have following Xpath for the element

//table[contains(@id,'Field')]//tr[td//span[text()='Code']]/preceding-sibling::*[1]/td//select[contains(@id,'GSRCH_FLT')]/option[text()='not=']

可以在浏览器中正确识别元​​素.但是当我使用以下webdriver方法进行验证

which is identifying the element properly in the browser. But when I am using the following webdriver method to verify it

driver.findElement(By.xpath("//table[contains(@id,'Field')]//tr[td//span[text()='Code']]/preceding-sibling::*[1]/td//select[contains(@id,'GSRCH_FLT')]/option[text()='not=']")).isDisplayed();

它返回false,因为它没有显示在框中.

its returning false since it is not getting displayed in the box.

您能告诉我替代方法吗?

Can u tell me the alternative for this.

推荐答案

您要:

private boolean isElementPresent(WebDriver driver, By by){
    return driver.findElements(by).count != 0;
}

findElements()对此要比findElement()更好,因为如果元素不存在,它不会等待.如果您在启用隐式等待的情况下运行,则findElement()将在寻找该元素时超时(这是您要捕获的异常),并且需要一段时间.

findElements() is better for this than findElement() because it won't wait if the element isn't present. If you're running with implicit waits turned on, findElement() will time out looking for the element (that's the exception you're catching), and it will take a while.

这篇关于Webdriver中selenium.isElementPresent的替代方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆