如何获取仅在页面中可见的webElements列表

时间:2016-02-11 12:48:38

标签: selenium automation webdriver

我想获取当前页面上显示的元素列表,因为某些内容隐藏在我不想访问的页面中。

2 个答案:

答案 0 :(得分:1)

假设您使用的是Java,您可以使用ExpectedConditions并执行类似的操作,

WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 300/*timeOutInSeconds*/);
ExpectedCondition<List<WebElement>> condition = ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("foo")) 
List<WebElement> allVisibleElements = wait.until(condition);

答案 1 :(得分:0)

public static void logVisible(WebDriver driver, String couldNotFind) {



     logger.error("Could not find element "+couldNotFind+", but here is what was actually on the page");

     driver.findElements(By.xpath("//*[self::div or self::input or self::li  or self::ul or self::button]")).stream()
    .filter(s -> s.isDisplayed())
    .forEach(s -> logger.error(String.format("Visible : Id:%s Tag:%s Class:%s Text:%s",
      s.getAttribute("id"), s.getTagName(), s.getAttribute("class"), s.getText()).replaceAll("  ", " ").replaceAll("\n", " ")));



 }