Example usage for org.openqa.selenium By.ByTagName By.ByTagName

List of usage examples for org.openqa.selenium By.ByTagName By.ByTagName

Introduction

In this page you can find the example usage for org.openqa.selenium By.ByTagName By.ByTagName.

Prototype

public ByTagName(String tagName) 

Source Link

Usage

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 w w.  jav a2 s .c om
            .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());
    }

}