wait.until(ExpectedConditions.visibilityOf Element1 或 Element2)

新手上路,请多包涵

我想将 wait.until(ExpectedConditions) 与两个元素一起使用。我正在运行测试,我需要 WebDriver 等待 Element1 或 Element2 出现。然后我需要选择先出现的人。我试过了:

 WebDriverWait wait = new WebDriverWait(driver, 60);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[@class='....']"))) || wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='... ']")));

        // Then I would need:

String result = driver.findElement(By.xpath("...")).getText() || driver.findElement(By.xpath("...")).getText();

总而言之,我需要等到两个元素中的任何一个出现。然后选择出现的人(他们不能同时出现)请帮忙。

原文由 user2058100 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 423
2 个回答

不幸的是,没有这样的命令。你可以通过 try and catch 来克服这个问题,或者我会更好地推荐你使用开源 Ruby 库 Watir

原文由 CHEBURASHKA 发布,翻译遵循 CC BY-SA 4.0 许可协议

现在有一个本机解决方案,即 or 方法, 请查看文档

你像这样使用它:

 driverWait.until(ExpectedConditions.or(
    ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.something")),
    ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.anything"))));

原文由 dialex 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题