Webdriver中selenium.isElementPresent的替代方法是什么

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.