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:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.Pagination.PaginationHandler18.java

License:Apache License

public void processSlickGuns(WebDriver driver) {
    try {//from   w  w w.ja  va2s . c  o  m
        int page = 0;
        String str1 = "<html><body><p>";
        while (true) {
            page++;
            List<WebElement> ele = driver.findElements(By.tagName("a"));
            for (WebElement we : ele) {
                str1 += "<a href='" + (String) we.getAttribute("href") + ">" + (String) we.getAttribute("href")
                        + "</a><br>";
            }
            try {
                // check if the next page is present
                // WebElement page_num = driver.findElement(By.xpath("//a[text()='"+ page +"']"));
                String pagestr = "page=" + page;
                String s1 = "a[href*='" + pagestr + "']";
                WebElement page_num = driver.findElement(By.cssSelector(s1));
                page_num.click();
            } catch (NoSuchElementException ex) {
                // end of pagination
                break;
            }
        }
        JavascriptExecutor js = (JavascriptExecutor) driver;
        str1 += "</p></body></html>";
        js.executeScript("document.body.innerHTML=arguments[0]", str1);
    } catch (NoSuchElementException ex) {
        // no pagination found
    }
}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.Pagination.PaginationHandler18.java

License:Apache License

public void vci_classifieds(WebDriver driver) {

    driver.manage().window().maximize();
    //to hover over the Ad Category button and click it
    driver.findElement(By.xpath("/html/body//a[@id='categorylink']")).click();
    //Hover over to firearm category and click Handguns
    WebElement element1 = driver.findElement(By.xpath("/html/body/div[3]/div/div/div[3]/ul/li[4]/a"));
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", element1);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    List<WebElement> w1 = driver.findElements(By.xpath("html/body//div[@id='adlist']/table[3]//a"));
    for (WebElement ele : w1) {
        String str1;/*from w  w w  .  j  av  a  2s  .com*/
        str1 = ele.getAttribute("href");
        int begin = str1.indexOf("(") + 2;
        if (begin < 0)
            continue;
        int end = str1.indexOf(")") - 1;
        if (end < 0)
            continue;
        String adId = str1.substring(begin, end);
        String url = "https://www.vci-classifieds.com/a-getad.php?id=" + adId;
        executor.executeScript("$.get(arguments[0], function(data) { $('#fb-root').html(data); } );", url);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    element1 = driver.findElement(By.xpath("/html/body/div[3]/div/div/div[3]/ul/li[5]/a"));
    executor.executeScript("arguments[0].click();", element1);
}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.Pagination.PaginationHandler18.java

License:Apache License

public void processDriver(WebDriver driver) {
    String url = driver.getCurrentUrl();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    // Charan/*  w w  w  .j a v  a2  s .co m*/
    if (url.matches("^http://([a-zA-Z0-9]+.)*armslist.com/classifieds/\\S*")) {
        processArmsList(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*impactguns.com/\\S+.aspx")) {
        processImpactGuns(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*iwanna.com/\\S*")) {
        processIwana(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*budsgunshop.com/\\S*")) {
        processBudgunshop(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*buyusedguns.net/\\S*")) {
        processBuyusedguns(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*cheaperthandirt.com/\\S*")) {
        processCheaperthandirt(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*davidsonsinc.com/\\S*") && processDavidSonic) {
        int size = driver.findElements(By.xpath("html/body//div[9]/a")).size();
        if (size > 0) {
            driver.findElement(By.xpath("html/body//div[9]/a")).click();
        }
        processDavidSonic = false;
    }
    //charan ends
    //sharan
    if (url.matches("^http://([a-zA-Z0-9]+.)*boards.4chan.org/k/\\S*")) {
        process4chan(driver);
    }
    if (url.matches("^http://([a-zA-Z0-9]+.)*tennesseegunexchange.com/\\S*")) {
        processtenness(driver);
    }
    if (url.matches(
            "^http://([a-zA-Z0-9]+.)*zidaho.com/category/(\\S*gun\\S*)|(\\S*Rifle\\S*)|(\\S*Ammunition\\S*)|(\\S*arms\\S*)|(\\S*Black-Powder\\S*)")) {
        processzidaho(driver);
    }
    if (url.matches("^http://([a-zA-Z0-9]+.)*dallasguns.com/\\S*")) {
        processdallasguns(driver);
    }
    if (url.matches("^http://([a-zA-Z0-9]+.)*elpasoguntrader.com/\\S*")) {
        processelpaso(driver);
    }
    if (url.matches("^http://([a-zA-Z0-9]+.)*floridaguntrader.com/\\S*")) {
        processfloridagun(driver);
    }
    if (url.matches("^http://([a-zA-Z0-9]+.)*iguntrade.com/\\S*")) {
        processigun(driver);
    }
    if (url.matches("^http://([a-zA-Z0-9]+.)*gandermountain.com/guns/\\S*")) {
        processgander(driver);
    }
    // sharan ends
    // rakshith
    if (url.matches("^http://([a-zA-Z0-9]+.)*gunlistings.org/ohio-gun-classifieds/\\S*")) {
        processGunListingsOhio(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*wikiarms.com/\\S*")) {
        processWikiArms(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*nextechclassifieds.com/\\S*")) {
        processNextechClassifieds(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*shooterswap.com/\\S*")) {
        processShooterSwap(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*slickguns.com/\\S*")) {
        processSlickGuns(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*gunlistings.org/\\S*")) {
        processGunListings(driver);
    }
    // rakshith ends
    // Mithun
    if (url.matches("^http://([a-zA-Z0-9]+.)*arguntrader.com/\\S*")) {
        argunTrader(driver);
    }

    if (url.matches("^http://([a-zA-Z0-9]+.)*vci-classifieds.com/\\S*")) {
        vci_classifieds(driver);
    }
    // Mithun ends
}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.Pagination.PaginationHandler9.java

License:Apache License

public void processDriver(WebDriver driver) {
    List<String> accumulatedData = new ArrayList<>(); //used to keep image tags from each page after click
    boolean paginationFound = false;
    boolean nextPageFound = false;

    System.err/*from w  ww . j av a2 s .c o m*/
            .println("DallasGunsPaginationHandler: Entered default dallasguns.com/guns_online page!!!!!!!!!,");
    String startPage = driver.getCurrentUrl();

    driver.findElement(By.tagName("body")).getAttribute("innerHTML");
    Configuration conf = NutchConfiguration.create();

    try {
        long end = System.currentTimeMillis() + 5000;
        while (System.currentTimeMillis() < end) {
            //Check if this page has pagination element, if so, need to process
            List<WebElement> testDivs = null;
            testDivs = driver.findElements(new By.ById("pagination"));
            if (testDivs != null && testDivs.size() > 0) {
                WebElement paginationTest = testDivs.get(0);
                if (paginationTest != null) {
                    paginationFound = true;
                    System.err.println("DallasGunsPaginationHandler: " + driver.getCurrentUrl()
                            + " found pagination!!!!!!!!!!!!!!!!");
                    break;
                }
            }
        }

        WebElement nextPage = null;
        List<WebElement> paginationDivs = null;
        List<WebElement> links = null;
        WebElement span = null;
        if (paginationFound) {
            paginationDivs = driver.findElements(new By.ById("pagination"));
            if (paginationDivs.size() > 0) {
                span = paginationDivs.get(0).findElement(new By.ByTagName("span"));
                if (span != null) {
                    // System.err.println("DallasGunsPaginationHandler: found span!!!!!");

                    links = span.findElements(new By.ByTagName("a"));
                    if (links != null && links.size() > 0) {
                        System.err.println("DallasGunsPaginationHandler: found pagination links!!!!!");
                        nextPage = links.get(links.size() - 1);
                        if (nextPage.getAttribute("title").equals("Next Page")) {
                            System.err.println("DallasGunsPaginationHandler: found Next Page link at "
                                    + driver.getCurrentUrl() + "!!!!!!");
                            nextPageFound = true;
                        }
                    }
                }
            }
        }

        while (nextPageFound) {
            nextPage.click();
            new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));
            System.err.println("DallasGunsPaginationHandler: Arrived at new URL: " + driver.getCurrentUrl()
                    + " from click!!!!!!!");

            List<WebElement> imgs = driver.findElements(new By.ByTagName("img"));

            if (imgs != null && imgs.size() > 0) {
                System.err.println("DallasGunsPaginationHandler: Found new Images at " + driver.getCurrentUrl()
                        + "!!!!!!!");
                for (int j = 0; j < imgs.size(); j++) {
                    String newImgSrc = imgs.get(j).getAttribute("src");
                    //String newImageTag = "<a href='"+newImgSrc+"'> another gun img </a>";
                    accumulatedData.add(newImgSrc);
                    //accumulatedData+=newImageTag;
                    System.err.println("DallasGunsPaginationHandler: updated accumulatedData with + "
                            + newImgSrc + "!!!!!!!!!");
                }
            }
            paginationDivs = driver.findElements(new By.ById("pagination"));
            if (paginationDivs.size() > 0) {
                span = paginationDivs.get(0).findElement(new By.ByTagName("span"));
                if (span != null) {
                    // System.err.println("DallasGunsPaginationHandler: found span!!!!!");
                    links = span.findElements(new By.ByTagName("a"));
                    if (links != null && links.size() > 0) {
                        System.err.println("DallasGunsPaginationHandler: found pagination links!!!!!");
                        nextPage = links.get(links.size() - 1);
                        if (nextPage.getAttribute("title").equals("Next Page")) {
                            System.err.println("DallasGunsPaginationHandler: found Next Page link at "
                                    + driver.getCurrentUrl() + "!!!!!!");
                            nextPageFound = true;
                        } else {
                            nextPageFound = false;
                        }
                    } else {
                        nextPageFound = false;
                    }
                } else {
                    nextPageFound = false;
                }
            } else {
                nextPageFound = false;
            }
        }

        if (accumulatedData.size() > 0) { //append images data to driver so that it can be processed by parser
            //navigating back to original page
            driver.get(startPage);
            System.err.println("DallasGunsPaginationHandler: navigated back to start page "
                    + driver.getCurrentUrl() + "!!!!!!");
            new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));
            System.err.println("DallasGunsPaginationHandler: appending new data!!!!!!!!");
            JavascriptExecutor jsx = (JavascriptExecutor) driver;
            for (String src : accumulatedData) {
                jsx.executeScript("var aTag = document.createElement('a'); aTag.setAttribute('href',\"" + src
                        + "\"); aTag.innerText = \"gun image\"; document.body.appendChild(aTag);");
                //jsx.executeScript("aTag.setAttribute('href','"+src+"');");
                //jsx.executeScript("aTag.innerText = 'gun image');");
                //jsx.executeScript("document.body.appendChild(aTag);");
            }

            //jsx.executeScript("document.body.innerHTML+= "+ accumulatedData+";");
        }

        System.err.println("DallasGunsIndexHandler: Finished Pagination Handler at " + driver.getCurrentUrl()
                + "!!!!!!!!!!!!");

    } catch (Exception e) {
        System.err.println("ERROR: DallasGunsIndexHandler @" + driver.getCurrentUrl() + e.getMessage());
    }

}

From source file:gov.nih.nci.firebird.selenium2.pages.components.tags.CommentsTag.java

License:Open Source License

private static WebElement getCommentsDiv(final WebDriver driver) {
    List<WebElement> commentsDivs = driver.findElements(By.id(COMMENTS_DIV_ID));
    return Iterables.find(commentsDivs, new Predicate<WebElement>() {
        @Override/*from  w w  w . j  a  v  a 2s  . co m*/
        public boolean apply(WebElement div) {
            return div.isDisplayed();
        }
    }, null);
}

From source file:imagedownload.Downloader.java

public void download() {
    System.out.println((new Date()).toString());

    WebDriver driver = new ChromeDriver();
    WebDriver.Window win = driver.manage().window();
    // get the full screen size (exculde desktop toolbar)
    win.maximize();//  ww w  .j  av  a  2s . co  m
    int screenHeight = win.getSize().getHeight();
    int screenWidth = win.getSize().getWidth();
    System.out.println("Screen: " + screenHeight + "*" + screenWidth);
    // set browser size and position it to bottom right
    win.setSize(new Dimension(browserWidth, browserHeight));
    System.out.println("Browser: " + win.getSize().getHeight() + "*" + win.getSize().getWidth());
    win.setPosition(new Point(screenWidth - browserWidth, screenHeight - browserHeight));
    // position the browser outside of windows
    if (hide) {
        win.setPosition(new Point(screenWidth, screenHeight));
    }

    String page = firstPage;
    boolean hasNextPage = true;
    int pageCnt = 0;
    while (hasNextPage && pageCnt < pageLimit) {
        pageCnt++;
        if (visitedPages.get(page) != null) {
            System.out.println("Looped back. Quit download.");
            break;
        }
        visitedPages.put(page, page);
        driver.get(page);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        hasNextPage = false;
        if (pagination) {
            // if no xpath configured, default search all <a> hyperlinks
            if (xpath == null || xpath.trim().equals("")) {
                xpath = determineNextPage(driver);
            }
            if (xpath != null && !xpath.trim().equals("")) {
                page = "";
                System.out.println("Xpath: " + xpath);
                List<WebElement> checkList = driver.findElements(By.xpath(xpath));
                for (WebElement elm : checkList) {
                    String text = elm.getAttribute("innerHTML");
                    for (String t : next) {
                        if (text.equals(t)) {
                            hasNextPage = true;
                            page = elm.getAttribute("href");
                            break;
                        }
                    }
                }
                System.out.println("Next page: " + hasNextPage + ", " + page);
            }
        }

        List<WebElement> imgList = driver.findElements(By.tagName("img"));
        List<String> filteredList = new ArrayList<String>();
        for (WebElement img : imgList) {
            if (img.getAttribute("src") == null) {
                continue;
            }
            if (duplicateFilter) {
                if (savedImages.get(img.getAttribute("src")) != null) {
                    continue;
                }
            }
            if (minSizeFilter) {
                if (img.getSize().getHeight() < minHeight || img.getSize().getWidth() < minWidth) {
                    continue;
                }
            }
            filteredList.add(img.getAttribute("src").trim());
        }

        int cnt = 0;
        InputStream imgIn = null;
        OutputStream imgOut = null;
        for (String location : filteredList) {
            cnt++;
            try {
                String[] urlArr = location.split("\\/");
                String[] nameArr = urlArr[urlArr.length - 1].split("\\.");
                String ext = nameArr[nameArr.length - 1];
                String filePath = config.getProperty("output.folder") + config.getProperty("output.namePrefix")
                        + "_" + pageCnt + "_" + cnt + "." + ext;
                System.out.println("File location: " + location);
                System.out.println("File name: " + urlArr[urlArr.length - 1]);
                System.out.println("File ext: " + nameArr[nameArr.length - 1]);
                System.out.println("Output: " + filePath);

                driver.get(location);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                WebElement image = driver.findElement(By.tagName("img"));
                System.out.println("Image element: " + image.getAttribute("src"));

                AutoSave.save(driver, image, filePath);
                savedImages.put(location, location);
            } finally {
                try {
                    if (imgIn != null) {
                        imgIn.close();
                    }
                    if (imgOut != null) {
                        imgOut.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    driver.quit();
    System.out.println((new Date()).toString());
}

From source file:imagedownload.Downloader.java

private String determineNextPage(WebDriver driver) {
    List<WebElement> checkList = driver.findElements(By.xpath("//a"));
    for (WebElement elm : checkList) {
        String text = elm.getAttribute("innerHTML");
        System.out.println("<a>: " + text);
        for (String t : next) {
            if (text.equalsIgnoreCase(t)) {
                String xpath = getXpath(driver, elm, "");
                System.out.println("matched, new xpath is: " + xpath);
                return xpath;
            }//from   w  w w .j a  va  2  s  .  c o  m
        }

    }
    return null;
}

From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java

License:Open Source License

/**
 * Tries to retrieve requested elements.
 *
 * <p>Tries to retrieve the requested amount of elements matching the given path.
 * Will retry until the amount matches or until the whole process times out.</p>
 *
 * @param by locator of an element// www.  j  a  va2s.c  o m
 * @return a list matching the searched specified element or <code>null</code> in case it couldn't be found.
 */
protected List<WebElement> getElements(final By by, final int expectedElementCount) {
    List<WebElement> elements;
    try {
        // will loop and try to retrieve the specified element until found or it times out.
        elements = new WebDriverWait(driver, timeout).until(new ExpectedCondition<List<WebElement>>() {

            @Override
            public List<WebElement> apply(WebDriver d) {
                try {
                    List<WebElement> elements = d.findElements(by);
                    if ((elements.size() > 0 && expectedElementCount == -1)
                            || (elements.size() == expectedElementCount)) {
                        takeScreenshot(by.toString());
                        return elements;
                    }
                    log.warn("Expecting {} element(s) for {} - trying again - found {} so far: {}",
                            expectedElementCount != -1 ? expectedElementCount : "at least 1", by,
                            elements.size(), elements);
                    takeScreenshot(by.toString() + "_wrongCount");
                    return null;
                } catch (NoSuchElementException e) {
                    takeScreenshot(by.toString() + "_notFound");
                    return null;
                }
            }
        });
    } catch (TimeoutException e) {
        log.error("Could not retrieve " + (expectedElementCount != -1 ? expectedElementCount : "at least 1")
                + " elements by path " + by + " : " + e.getMessage());
        return Collections.emptyList();
    } catch (StaleElementReferenceException s) {
        // re-trying on StaleElementReferenceExceptions: see http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
        log.info("{} when accessing element {} - trying again", s.toString(), by);
        elements = getElements(by, expectedElementCount);
    }
    return elements;
}

From source file:injector.psm.googlecode.GoogleCodeInjector.java

public GoogleCodeProject injectProject(String projectURL) {
    GoogleCodeProject project = GooglecodeFactory.eINSTANCE.createGoogleCodeProject();

    WebDriver driver = getDriver();
    driver.get(projectURL);//from   w  w w  . ja v  a 2s  . co  m

    // Info from the header
    WebElement headerElement = driver.findElement(By.className("headbg"));

    WebElement nameElement = headerElement.findElement(By.id("pname"));
    String name = nameElement.getText();
    project.setName(name);

    WebElement summaryElement = headerElement.findElement(By.id("project_summary_link"));
    String summary = summaryElement.getText();
    project.setSummary(summary);

    // Info from left column
    WebElement leftElement = driver.findElement(By.className("pscolumnl"));

    // TODO fix this
    /*WebElement starElement = leftElement.findElement(By.id("star_count"));
      int stars = Integer.valueOf(starElement.getText());
      project.setStars(stars);*/

    List<WebElement> psmetaElements = driver.findElements(By.className("psmeta"));
    if (psmetaElements.size() > 2) {
        WebElement licenseElement = psmetaElements.get(2);
        String license = licenseElement.getText();
        project.setLicense(license);
    }

    WebElement projectLabelsElement = leftElement.findElement(By.id("project_labels"));
    List<WebElement> labelElements = projectLabelsElement.findElements(By.className("label"));
    for (WebElement labelElement : labelElements) {
        String labelName = labelElement.getText();
        GoogleProjectLabel projectLabel = getProjectLabel(labelName);
        project.getLabels().add(projectLabel);
    }

    List<WebElement> userElements = leftElement.findElements(By.className("userlink"));
    for (WebElement userElement : userElements) {
        String username = userElement.getText();
        GoogleUser user = getUser(username);
        project.getMembers().add(user);
    }

    // Info from the main column (right)
    WebElement rightElement = driver.findElement(By.id("wikicontent"));
    String description = rightElement.getText();
    project.setDescription(description);

    // The rest        
    GoogleIssueTracker issueTracker = GooglecodeFactory.eINSTANCE.createGoogleIssueTracker();
    issueTracker.setUrl(projectURL + "/issues");
    project.setIssueTracker(issueTracker);

    GoogleWiki wiki = GooglecodeFactory.eINSTANCE.createGoogleWiki();
    project.setWiki(wiki);

    driver.close();

    return project;

}

From source file:injector.psm.googlecode.GoogleCodeInjector.java

public void injectIssues(String projectURL) {
    WebDriver driver = getDriver();

    // TODO the URL param sets the limit to 1000, check if there are more!
    String issuesURL = projectURL + "/issues/list?num=1000&start=0";
    driver.get(issuesURL);// ww w. j  a v a2  s  .  c o  m
    List<WebElement> issueElements = driver.findElements(By.className("ifOpened"));

    int counter = 0;
    for (WebElement issue : issueElements) {
        // Navigating to issue page           
        WebElement aIssue = issue.findElement(By.tagName("a"));
        String href = aIssue.getAttribute("href");
        GoogleIssue googleIssue = injectIssue(href);
        issues.put(googleIssue.getId(), googleIssue);
        if (++counter >= MAX_ELEMS)
            break;
    }
}