【问题标题】:WebElement.isDisplayed() works differently on different machines?WebElement.isDisplayed() 在不同机器上的工作方式不同?
【发布时间】:2020-03-10 13:06:58
【问题描述】:

我有一段代码获取一个 web 元素并检查该元素是否显示。

代码:

WebElement element = getDriver().findElement(By.linkText("Expand"));
 if(element.isDisplayed()){
                printInfo("Expand is displayed");
            }
  else{
   printInfo("Expand is NOT displayed");
  }

同一段代码在我的本地计算机上的相同环境中运行良好(打印“显示扩展”)。但是,在远程机器上(使用 Jenkins),在同一环境中使用相同的代码,它会失败并执行 else 块(打印“不显示展开”)。

我可以在执行过程中直观地看到应用程序上显示的元素,也不存在同步问题。

为什么同一段代码在同一环境下在不同机器上表现不同?

两台机器上的操作系统:MacOS。

两台机器上的浏览器:Firefox。

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    isDisplayed() 不保证元素是否在DOM Tree 中可见。相反,您需要在try-catch{} 块内为visibilityOfElementLocated() 诱导WebDriverWait,您可以使用以下Locator Strategy

    try {
      new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Expand")));
      printInfo("Expand is visible");
    }
    catch(TimeoutException e) {
      printInfo("Expand is NOT visible");
    }
    

    参考

    您可以在以下网址找到关于不同ExpectedConditions的相关详细讨论:

    【讨论】:

      猜你喜欢
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      相关资源
      最近更新 更多