Example usage for org.openqa.selenium WebDriver findElements

List of usage examples for org.openqa.selenium WebDriver findElements

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver findElements.

Prototype

@Override
List<WebElement> findElements(By by);

Source Link

Document

Find all elements within the current page using the given mechanism.

Usage

From source file:com.zhao.crawler.jd.QunaerGoodsList.java

License:Open Source License

@Override
public void addGoods(Page page) {
    WebDriver driver = null;
    try {/*from   w  w w . j  a v a  2s  . co m*/

        String selector = "html";
        //li.premium-pricecube
        driver = PageUtils.getWebDriver(page);
        List<WebElement> eles = driver.findElements(By.cssSelector(selector));
        if (!eles.isEmpty()) {
            for (WebElement ele : eles) {
                Goods g = new Goods();
                g.setPlatform(Platform.Qunaer);// ?
                // 
                //               String priceStr = ele.findElement(By.className("p-price"))
                //                     .findElement(By.className("J_price"))
                //                     .findElement(By.tagName("i"))
                //                     .getText();
                //                  if (Tools.notEmpty(priceStr)) {
                //                  g.setPrice(Float.parseFloat(priceStr));
                //               } else {
                //                  g.setPrice(-1f);
                //               }
                // ???
                g.setName(ele.getText());
                //               // ?
                //               g.setUrl(ele.findElement(By.className("p-name"))
                //                     .findElement(By.tagName("a"))
                //                     .getAttribute("href"));
                //               // 
                //               String commitStr = ele
                //                     .findElement(By.className("p-commit"))
                //                     .findElement(By.tagName("a"))
                //                     .getText();
                //               if (Tools.notEmpty(commitStr)) {
                //                  commitStr ="100";
                //                  g.setCommit(Integer.parseInt(commitStr));
                //               } else {
                //                  g.setCommit(-1);
                //               }

                add(g);
            }
        } else {
            System.out.println("else is empty");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}

From source file:daveayan.gherkinsalad.components.core.BaseBrowserElement.java

License:Open Source License

public Elements findElements(final By by) {
    if (by == null) {
        error("Cannot find element on the page with a null element locator");
        return Elements.nullInstance();
    }/*from   ww w .j  a v  a  2s. co  m*/
    if (browser.driver() instanceof NullWebDriver) {
        throw new AssertionError("Cannot find any element '" + by + "' on a NullWebDriver");
    }
    Wait<WebDriver> wait = new FluentWait<WebDriver>(browser.driver())
            .withTimeout(Config.seconds_timeout, TimeUnit.SECONDS)
            .pollingEvery(Config.seconds_poll_interval, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);
    List<WebElement> _webElements;
    try {
        _webElements = wait.until(new Function<WebDriver, List<WebElement>>() {
            public List<WebElement> apply(WebDriver driver) {
                return driver.findElements(by);
            }
        });
    } catch (TimeoutException toe) {
        return Elements.nullInstance();
    }
    Elements elements = convertToElements(_webElements, by);
    return elements;
}

From source file:de.codecentric.janus.plugin.suite.AbstractStep.java

License:Apache License

public void waitUntilPageContainsText(final String cssSelector, final String text) {
    waitUntil(new ExpectedCondition<Boolean>() {
        @Override/*from   w w  w  .jav a  2s. c  om*/
        public Boolean apply(@Nullable WebDriver driver) {
            List<WebElement> elements = driver.findElements(By.cssSelector(cssSelector));

            for (WebElement element : elements) {
                if (element.isDisplayed() && element.getText().contains(text)) {
                    return true;
                }
            }

            return false;
        }
    });
}

From source file:de.knowwe.uitest.UITestUtils.java

License:Open Source License

public static void logIn(WebDriver driver, String username, String password, UseCase use, WikiTemplate template)
        throws InterruptedException {
    List<WebElement> elements = null;
    if (use == UseCase.LOGIN_PAGE) {
        String idLoginElement = template == WikiTemplate.haddock ? "section-login" : "logincontent";
        elements = driver.findElements(By.id(idLoginElement));
    } else if (use == UseCase.NORMAL_PAGE) {
        if (template == WikiTemplate.haddock) {
            new WebDriverWait(driver, 20)
                    .until(ExpectedConditions.elementToBeClickable(By.className("userbox")));
            driver.findElement(By.className("userbox")).click();
            Thread.sleep(1000); //Animation
        }//from   w  w w  . j ava  2 s . co  m
        String loginSelector = template == WikiTemplate.haddock ? "a.btn.btn-primary.btn-block.login"
                : "a.action.login";
        new WebDriverWait(driver, 20)
                .until(ExpectedConditions.elementToBeClickable(By.cssSelector(loginSelector)));
        elements = driver.findElements(By.cssSelector(loginSelector));
    }

    if (elements == null) {
        throw new NullPointerException("No Login Interface found.");
    } else if (elements.isEmpty()) {
        return; // already logged in
    }

    elements.get(0).click();
    driver.findElement(By.id("j_username")).sendKeys(username);
    driver.findElement(By.id("j_password")).sendKeys(password);
    driver.findElement(By.name("submitlogin")).click();
    String logoutSelector = template == WikiTemplate.haddock ? "a.btn.btn-default.btn-block.logout"
            : "a.action.logout";
    new WebDriverWait(driver, 10)
            .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(logoutSelector)));
    new WebDriverWait(driver, 10)
            .until(ExpectedConditions.presenceOfElementLocated(By.id("edit-source-button")));
}

From source file:de.knowwe.uitest.UITestUtils.java

License:Open Source License

private static boolean isLoggedIn(WebDriver driver, WikiTemplate template) {
    String logoutSelector = template == WikiTemplate.haddock ? "a.btn.btn-default.btn-block.logout"
            : "a.action.logout";
    return !driver.findElements(By.cssSelector(logoutSelector)).isEmpty();
}

From source file:de.knowwe.uitest.UITestUtils.java

License:Open Source License

public static void awaitRerender(WebDriver driver, By by) {
    try {/*from  w  w w  . j  a  va2  s  . c  o m*/
        List<WebElement> elements = driver.findElements(by);
        if (!elements.isEmpty()) {
            new WebDriverWait(driver, 5).until(ExpectedConditions.stalenessOf(elements.get(0)));
        }
    } catch (TimeoutException ignore) {
    }
    new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfElementLocated(by));
}

From source file:de.knowwe.uitest.UITestUtils.java

License:Open Source License

private static boolean pageExists(WikiTemplate template, WebDriver driver) {
    if (template == haddock) {
        try {//from w  w  w.j a  v  a 2  s  .  com
            new WebDriverWait(driver, 5)
                    .until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("a.createpage")));
        } catch (Exception e) {
            // Element not present
        }
        return driver.findElements(By.cssSelector("a.createpage")).isEmpty();
    } else {
        try {
            new WebDriverWait(driver, 5).until(
                    ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("div.information a")));
        } catch (Exception e) {
            // Element not present
        }
        return driver.findElements(By.cssSelector("div.information a")).stream()
                .noneMatch(webElement -> Strings.containsIgnoreCase(webElement.getText(), "create it"));
    }
}

From source file:de.knowwe.uitest.UITestUtils.java

License:Open Source License

private static void createDummyPage(WikiTemplate template, WebDriver driver) throws IOException {
    WebElement href;// www  . ja v a2 s  .  c  om
    if (template == haddock) {
        href = driver.findElement(By.cssSelector("a.createpage"));
    } else {
        href = driver.findElements(By.cssSelector("div.information a")).stream()
                .filter(webElement -> Strings.containsIgnoreCase(webElement.getText(), "create it")).findFirst()
                .get();
    }
    href.click();
    enterArticleText(Strings.readFile("src/test/resources/Dummy.txt"), driver, template);

}

From source file:de.meethub.adapters.eventbrite.EventbriteAdapterServlet.java

License:Open Source License

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {

    final WebDriver d = new HtmlUnitDriver();
    try {/* w  ww  .j a v  a2s . c om*/
        d.get(this.baseUrl);
        new WebDriverWait(d, 10)
                .until(ExpectedConditions.presenceOfElementLocated(By.className("js-event-list-container")));
        final List<WebElement> eventElements = d.findElements(By.className("event-card__description"));
        final Calendar calendar = this.convertToCalendar(eventElements);
        final CalendarOutputter outputter = new CalendarOutputter();
        response.setContentType("text/calendar;charset=UTF-8");
        outputter.output(calendar, response.getWriter());
    } catch (final ValidationException | ParseException e) {
        throw new ServletException(e);
    } finally {
        d.close();
    }

}

From source file:de.meethub.adapters.xing.XingAdapterServlet.java

License:Open Source License

private Calendar convertToCalendar(final WebDriver d) throws IOException, ParseException, ServletException {
    final Calendar ret = new Calendar();
    ret.getProperties().add(new ProdId("-//Meet-Hub Hannover//xing//DE"));
    ret.getProperties().add(Version.VERSION_2_0);
    ret.getProperties().add(CalScale.GREGORIAN);

    for (final WebElement eventElement : d.findElements(By.className("event-preview"))) {
        final VEvent event = new VEvent(
                new net.fortuna.ical4j.model.DateTime(this.extractStartTime(eventElement)),
                this.extractTitle(eventElement));
        final UidGenerator ug = new UidGenerator(ManagementFactory.getRuntimeMXBean().getName());
        event.getProperties().add(ug.generateUid());
        ret.getComponents().add(event);//from w  w  w .jav  a 2s  .c  o  m
    }

    return ret;
}