【问题标题】:isDisplayed() method always returns true for some elementsisDisplayed() 方法总是对某些元素返回 true
【发布时间】:2013-09-21 08:07:00
【问题描述】:

当输入字段为空时,我正在使用 Selenium 测试错误消息的外观。错误消息设计为输入元素的标签。 当消息不可见时,它具有属性“display: none;”

当我通过文本找到该消息并调用 isDisplayed() 方法时,即使消息不可见,它也始终返回 true。我在 Java 上编写测试,所以我没有 isVisible() 消息。

我试过方法 getAttribute("style"),但它返回空字符串。 方法 getCssValue("display") 返回 "block",即使在页面上它的值为 "none"

在调用 click() 方法后,我期待 ElementNotVisibleException,但什么也没发生!

有什么想法吗?解决方法?

这里是 HTML 示例:

<form id="from id" style="display: block;">

&lt;input id="input" name="input"&gt;

&lt;label for="input" generated="true" style="display: none;"&gt;Error text here.&lt;/label&gt;

</from>

【问题讨论】:

  • 您只是找到了错误的元素。尝试使用 css 或 xpath 选择器定位您的元素
  • 我猜您的选择器没有选择正确的元素(可能是与选择器匹配的较早元素)
  • 分享重现此问题的确切 HTML 和代码,并说明正在测试的浏览器。
  • 找到的元素是正确的。页面上只有一个元素带有该文本。我使用 Chrome 进行测试(可以在 firefox/ie 上运行)。
  • @user2796068 我相信这是错误的元素,因为您说它的样式属性中有display: none;,当您尝试使用 webdriver 返回该属性时,您会得到一个空字符串

标签: java selenium selenium-webdriver


【解决方案1】:

尝试使用 WebDriverWait() 来查找 WebElement,您可以等待元素的可见性:

/**
 * 
 * Get a Web Element using WebDriverWait()
 * 
 */

public WebElement getInputBox() throws TimeoutException {

    WebElement webElement = null;
    WebDriverWait driverWait = new WebDriverWait(5);

    // find an element using a By selector

    driverWait.until(
            ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#input")));

    webElement = driver.findElement(By.cssSelector("#input"));

    return webElement;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2012-05-15
    • 2012-11-19
    • 2020-11-17
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多