【问题标题】:@FindBy annotations cannot find the element, when element state is not visible@FindBy 注释找不到元素,当元素状态不可见时
【发布时间】:2019-05-24 13:12:26
【问题描述】:

@FindBy 注释找不到元素,当元素状态不可见时。我们正在编写一些 SEO 测试,这些元素在网页上不可见。

例如以下不起作用;

@CacheLookup
@FindBy(xpath = "//meta[@name='description']")
public WebElementFacade metaDescription;

但这行得通;

WebElement metaV2 = getDriver().findElement(By.xpath("//meta[@name='description']"));

它给出了一个类似的错误;

org.openqa.selenium.ElementNotVisibleException: Timed out after 15 seconds. Element not available

有什么想法吗?

谢谢

【问题讨论】:

  • 你在使用 findBy 注释时看到了什么错误?
  • @CacheLookup 正在缓存您最初查找的结果,并确保它不会再次尝试查找该元素。这可能会导致一些意想不到的副作用。
  • 如果您想等到某些东西可见,最好的选择是使用显式等待。
  • 没有@CacheLookup 也无法工作。

标签: selenium serenity-bdd cucumber-serenity findby


【解决方案1】:

WebElementFacade 期望元素在与其交互之前是可见的(许多标准 WebElement 方法也是如此)。如果要检查不可见元素,请使用 WebElement 或完全避免 @FindBy,例如

By META_V2 = By.xpath("//meta[@name='description']")
.
.
.
$(META_V2).shouldBePresent();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2019-10-06
    • 2018-11-16
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多