html - 是否可以有没有高度和宽度的可见元素?

标签 html css selenium selenium-webdriver dom

我一直在研究现有的 Selenium 自动化框架,其中有一段代码可以检查元素的宽度和高度。如果它们只有正值,那么它将是可交互的,例如单击、双击等。

public boolean isElementInteractable(WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String offsetWidth = js.executeScript("return arguments[0].offsetWidth;", element).toString();
    String offsetHeight = js.executeScript("return arguments[0].offsetHeight;", element).toString();

    if ((Integer.parseInt(offsetWidth) != 0 && Integer.parseInt(offsetHeight) != 0) && element.isDisplayed()) {
        return true;
    }
    return false;
}

在我看来,每个可见/可交互的元素都具有正的高度和宽度。我很困惑为什么要编写这段代码。

能否请您确认我的理解是否正确?如果没有,请帮助我了解可能的情况。

最佳答案

WebElement 必须有宽度和高度才能变得棘手,但在两种情况下它可以被认为是可见的:它具有正的 stroke-width 或它们具有正大小的子节点/文本节点

来自 WebDriver specifications Element displayedness

The element displayed algorithm is a boolean state where true signifies that the element is displayed and false signifies that the element is not displayed. To compute the state on element, invoke the Call(bot.dom.isShown, null, element).

来自 bot.dom.isShown_在 github 中(第 437 行)

function positiveSize(e) {
    var rect = bot.dom.getClientRect(e);
    if (rect.height > 0 && rect.width > 0) {
      return true;
    }
    // A vertical or horizontal SVG Path element will report zero width or
    // height but is "shown" if it has a positive stroke-width.
    if (bot.dom.isElement(e, 'PATH') && (rect.height > 0 || rect.width > 0)) {
      var strokeWidth = bot.dom.getEffectiveStyle(e, 'stroke-width');
      return !!strokeWidth && (parseInt(strokeWidth, 10) > 0);
    }
    // Zero-sized elements should still be considered to have positive size
    // if they have a child element or text node with positive size, unless
    // the element has an 'overflow' style of 'hidden'.
    return bot.dom.getEffectiveStyle(e, 'overflow') != 'hidden' &&
        goog.array.some(e.childNodes, function(n) {
          return n.nodeType == goog.dom.NodeType.TEXT ||
                 (bot.dom.isElement(n) && positiveSize(n));
        });
    }

关于html - 是否可以有没有高度和宽度的可见元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59661568/

相关文章:

javascript - 如何使用 jQuery 转到特定部分?

javascript - 使用 CSS3 动画制作幻灯片

html - CSS 网格 : How Auto placement algorithm works

node.js - Node selenium-webdriver "Error: Timed out waiting for the WebDriver server at"

javascript - 如何在 alert() 之后给予 focus()?

javascript - 带有 hammerjs 滑动检测的输入类型范围在移动 safari 上不可用

python - 如何使用 Selenium 和 Python 处理 try 循环中的错误

python - 在 Python 中编辑多行字符串

html - 使用 Bootstrap 以响应方式添加图像和其他元素

python - 无法使用带有 selenium 的 python3 在表中找到链接