Example usage for org.openqa.selenium JavascriptExecutor executeScript

List of usage examples for org.openqa.selenium JavascriptExecutor executeScript

Introduction

In this page you can find the example usage for org.openqa.selenium JavascriptExecutor executeScript.

Prototype

Object executeScript(String script, Object... args);

Source Link

Document

Executes JavaScript in the context of the currently selected frame or window.

Usage

From source file:evaluation.ExperimentRunner.java

License:Apache License

private void runUrl(String url) {
    System.out.println("Loading " + url);
    driver.get(url);/*from   w  w  w  . j a  v  a2 s .c o m*/
    System.out.println("Done loading " + url);

    // System.out.println("Trying to find pElement..");
    // WebElement pElement =
    // driver.findElement(By.className("jalangiFF-p"));
    // System.out.println("pElement: "+pElement);
    // pElement.click();
    // System.out.println("Have clicked pElement");

    // wait a little bit to allow additional js
    // code to be executed
    System.out.println("Waiting...");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }

    System.out.println("Ending execution...");
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    try {
        jse.executeScript(endExecCode, "");
    } catch (Exception ex) {
        System.out.println("!!!exception occurred, analysis.json file is not generated.");
        System.out.println(ex);
    }

    System.out.println("Will sleep a bit..");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    driver.close();
    System.out.println("Done :-)");
}

From source file:evaluation.ExperimentRunner.java

License:Apache License

public void autoClick() throws Exception {
    System.out.println("start auto-clicking");
    String content = readFile(/*w ww.j  av  a2 s . c  om*/
            "/Users/jacksongl/macos-workspace/research/jalangi/github_dlint/gitprojects/jalangi-dlint/src/java/evaluation/autoClick.js");
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript(content, "");
}

From source file:evaluation.ExperimentRunner.java

License:Apache License

public void systematicExploreClick() throws Exception {
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    for (int i = 0; i < 5; i++) {
        autoClick();/*from   ww w  .  ja v a 2  s .  c o  m*/
        System.out.println("auto scroll down");
        jse.executeScript("window.scrollBy(0,250)", "");
    }
}

From source file:evaluation.OctaneExperimentRunner.java

License:Apache License

public void testBenchmark(String benchmark) throws Exception {
    driver.get(baseUrl + "index_" + benchmark + ".html?auto=1");
    (new WebDriverWait(driver, maxWaitTime))
            .until(ExpectedConditions.textToBePresentInElementLocated(By.id("main-banner"), "Score"));

    System.out.println("Ending execution...");
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    try {/*from   w ww . j ava 2 s .co m*/
        jse.executeScript(endExecCode, "");
    } catch (Exception ex) {
        System.out.println("!!!exception occurred, analysis.json file is not generated.");
        System.out.println(ex);
    }

    System.out.println("Will sleep a bit..");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

From source file:facebookfriendsoffriends.FacebookFriendsOfFriends.java

public void Scroll_Page_Down() throws Exception {
    try {//  w  w  w.j  av a 2  s  .c  o m
        Thread.sleep(1000);
        JavascriptExecutor jse = (JavascriptExecutor) this.driver;
        jse.executeScript("window.scrollBy(0,10000)", "");
    } catch (Exception e) {
    }

}

From source file:facebookfriendsoffriends.FindFriends.java

public void scrollPageDown() throws Exception {
    try {//from w  w  w  . j a  v  a 2 s .c  o m
        JavascriptExecutor jse = (JavascriptExecutor) this.driver;
        jse.executeScript("window.scrollBy(0,1000)", "");
        Thread.sleep(1000);
    } catch (Exception e) {
    }

}

From source file:fw.basic.wrap.ExtBy.java

License:MIT License

@Override
public List<WebElement> findElements(final SearchContext context) {
    if (context instanceof JavascriptExecutor) {
        final JavascriptExecutor javascriptContext = (JavascriptExecutor) context;
        injectSizzle(javascriptContext);
        return (List<WebElement>) javascriptContext.executeScript("return Sizzle(arguments[0])", selector);
    }/* w  ww  . ja  v a2s . c om*/

    if (context instanceof WebElement && context instanceof WrapsDriver) {
        final WrapsDriver wrapsdriverContext = (WrapsDriver) context;
        final WebDriver driver = wrapsdriverContext.getWrappedDriver();
        final JavascriptExecutor javascriptContext = (JavascriptExecutor) driver;
        injectSizzle(javascriptContext);
        if (driver instanceof JavascriptExecutor) {
            return (List<WebElement>) javascriptContext
                    .executeScript("return Sizzle(arguments[0], arguments[1])", selector, context);
        }
    }
    throw new UnsupportedOperationException(
            "Can only search using sizzle on JavascriptExecutors, or WebElements which wrap JavascriptExecutors");
}

From source file:google.TestGoogle.java

/**
 * @param args the command line arguments
 *///from  w w w  . j  a v a  2s  .  co  m
public static void main(String[] args) throws InterruptedException {
    System.setProperty("webdriver.chrome.driver",
            "D:\\GitHub\\java\\Gradle\\SeleniumTest\\exe\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://bbs.sgcn.com/member.php?mod=logging&action=login");
    Thread.sleep(5000); // Let the user actually see something!
    //WebElement searchBox = driver.findElement(By.name("q"));
    //searchBox.sendKeys("ChromeDriver");
    //searchBox.submit();

    driver.findElement(By.id("u_u")).sendKeys("?");
    driver.findElement(By.id("u_p")).sendKeys("diesel");
    driver.findElement(By.className("u_s1")).submit();
    Thread.sleep(5000);
    driver.findElement(By.className("bind_skip")).submit();

    driver.get("http://bbs.sgcn.com/thread-15716879-1-1.html");
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    WebElement we = driver.findElement(By.id("fastpostsubmit"));
    jse.executeScript("arguments[0].scrollIntoView(true);", we);
    driver.findElement(By.id("fastpostsubmit")).click();
    Thread.sleep(5000); // Let the user actually see something!
    driver.quit();
}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.MultiplePatterns.CrawlHandler.java

License:Apache License

/**
 *  handles data behind web form, pagination, ajax based interaction for seed urls
 *///  ww w. ja  v  a2  s.c om

public void processDriver(WebDriver driver) {
    StringBuffer sBuffer = new StringBuffer();
    String htmlpage = "<html><body>";
    System.out.println("reading url " + driver.getCurrentUrl());
    if (driver.getCurrentUrl().equals("http://www.wikiarms.com/")) {
        // handling pagination

        driver.manage().window().maximize();
        List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }

        WebElement ele = driver.findElement(By.xpath("//*[@id='products-table']/div/ul//a[@rel='next']"));
        for (int i = 0; ele != null; i++) {

            try {
                ele.click();
                System.out.println(driver.getCurrentUrl());
                Thread.sleep(1000);
                List<WebElement> anchorTags2 = driver.findElements(By.xpath("//*[@id='products-table']//a"));

                for (WebElement anchorTag2 : anchorTags2) {
                    htmlpage += "<a href='" + anchorTag2.getAttribute("href") + "'>test</a>";
                    sBuffer.append(anchorTag2.getAttribute("href"));
                    sBuffer.append("\n");
                }
                ele = driver.findElement(By.xpath("//*[@id='products-table']/div/ul//a[@rel='next']"));

            } catch (Exception e) {
                ele = null;

            }

        }
        htmlpage += "</body></html>";
        System.out.println("html is " + htmlpage);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        //return htmlpage;

    } else if (driver.getCurrentUrl().equals("http://www.ksl.com/")) {
        // handling pagination and search guns

        driver.manage().window().maximize();
        WebElement loginWindow = driver
                .findElement(By.xpath("//*[@id='kslHeader']/div/div[1]/div/div[4]/a/span"));
        loginWindow.click();
        WebElement username = driver.findElement(By.className("ksl-header-search__input"));
        username.sendKeys("guns");
        WebElement loginBtn = driver
                .findElement(By.xpath("//*[@id='kslHeader']/div/div[1]/div/div[4]/div/form/button"));
        loginBtn.click();
        System.out.println(driver.getCurrentUrl());
        List pages = driver.findElements(
                By.xpath("//*[@id='___gcse_0']/div/div/div/div[5]/div[2]/div/div/div[2]/div[11]//div"));
        int size = pages.size();
        List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }
        System.out.println(size);
        if (size > 0) {
            for (int i = 2; i < size; i++) {
                System.out.println("inside for loop");
                try {
                    driver.findElement(By.xpath(
                            "//*[@id='___gcse_0']/div/div/div/div[5]/div[2]/div/div/div[2]/div[11]/div/div[" + i
                                    + "]"))
                            .click();
                    Thread.sleep(1000);
                    List<WebElement> anchorTags2 = driver
                            .findElements(By.xpath("/html/body/div[1]/div[2]/div[2]/div[3]/div/div[2]//a"));

                    for (WebElement anchorTag2 : anchorTags2) {
                        htmlpage += "<a href='" + anchorTag2.getAttribute("href") + "'>test</a>";
                        sBuffer.append(anchorTag2.getAttribute("href"));
                        sBuffer.append("\n");
                    }
                } catch (Exception e) {

                }
            }

        }

        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);

    } else if (driver.getCurrentUrl().equals("http://www.kyclassifieds.com/")) {

        // handling pagination and search guns

        System.out.println("final else loop");
        driver.manage().window().maximize();
        WebElement searchField = driver.findElement(By.xpath("//*[@id='s']"));
        searchField.sendKeys("guns");
        WebElement searchButton = driver.findElement(By.xpath("//*[@id='go']"));
        searchButton.click();
        System.out.println(driver.getCurrentUrl());
        List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }
        WebElement ele = driver.findElement(By.className("next page-numbers"));
        for (int i = 0; ele != null; i++) {

            try {
                ele.click();
                System.out.println(driver.getCurrentUrl());
                Thread.sleep(1000);
                List<WebElement> anchorTags2 = driver
                        .findElements(By.xpath("/html/body/div/div[4]/div/div/div[2]//a"));

                for (WebElement anchorTag2 : anchorTags2) {
                    htmlpage += "<a href='" + anchorTag2.getAttribute("href") + "'>test</a>";
                    sBuffer.append(anchorTag2.getAttribute("href"));
                    sBuffer.append("\n");
                }
                ele = driver.findElement(By.className("next page-numbers"));

            } catch (Exception e) {
                ele = null;

            }

        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);

    } else if (driver.getCurrentUrl().equals("http://www.iguntrade.com/")) {
        // handling paginations

        List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }
        WebElement ele = driver.findElement(By.className("searchPaginationNext list-last"));
        for (int i = 0; ele != null; i++) {

            try {
                ele.click();
                System.out.println(driver.getCurrentUrl());
                Thread.sleep(1000);
                List<WebElement> anchorTags2 = driver.findElements(By.xpath("//*[@id='main']//a"));

                for (WebElement anchorTag2 : anchorTags2) {
                    htmlpage += "<a href='" + anchorTag2.getAttribute("href") + "'>test</a>";
                    sBuffer.append(anchorTag2.getAttribute("href"));
                    sBuffer.append("\n");
                }
                ele = driver.findElement(By.className("searchPaginationNext list-last"));

            } catch (Exception e) {
                ele = null;

            }

        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);

    } else if (driver.getCurrentUrl().equals("http://www.gowilkes.com/")) {
        // handling search guns

        WebElement searchField = driver.findElement(By.xpath("//*[@id='search_box']"));
        searchField.sendKeys("guns");
        WebElement classifiedRadioBtn = driver
                .findElement(By.xpath("//*[@id='search_container']/form/div/div[5]/input"));
        classifiedRadioBtn.click();
        WebElement searchButton = driver
                .findElement(By.xpath("//*[@id='search_container']/form/table/tbody/tr/td[2]/span"));
        searchButton.click();
        System.out.println(driver.getCurrentUrl());
        List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }
        List<WebElement> anchorTagGuns = driver
                .findElements(By.xpath("//*[@id='content']/tbody/tr/td[1]/div[2]/table"));
        for (WebElement anchorTag : anchorTagGuns) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);

    } else if (driver.getCurrentUrl().equals("http://www.floridagunclassifieds.com/")) {
        // handling scrolling of page

        for (int i = 0; i < 20; i++) {
            JavascriptExecutor jse = (JavascriptExecutor) driver;
            jse.executeScript("window.scrollBy(0,250)", "");
        }
        List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);
    } else if (driver.getCurrentUrl().equals("http://www.elpasoguntrader.com/")) {
        // handling pagination

        List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }

        WebElement nextField = driver
                .findElement(By.xpath("//*[@id='dj-classifieds']/div/div/div//a[@title='Next']"));
        for (int i = 0; nextField.isEnabled(); i++) {
            try {
                nextField.click();
                Thread.sleep(1000);
                List<WebElement> anchors = driver.findElements(By.xpath("//a"));

                for (WebElement anchorTag : anchors) {
                    htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
                    sBuffer.append(anchorTag.getAttribute("href"));
                    sBuffer.append("\n");
                }
                nextField = driver
                        .findElement(By.xpath("//*[@id='dj-classifieds']/div/div/div//a[@title='Next']"));
            } catch (Exception e) {
            }
        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);

    } else if (driver.getCurrentUrl().equals("http://www.classifiednc.com/")) {
        // handling gun link tap in left panel and pagination

        WebElement forSale = driver.findElement(By.xpath("//*[@id='body_con']/div[1]/div/div[2]/div[5]"));
        forSale.click();
        WebElement gunList = driver.findElement(By.xpath("//*[@id='catlist']/div[16]/a"));
        gunList.click();
        boolean nextBtn = driver
                .findElement(By.xpath(
                        "//*[@id='classifiedsContainer']/table/tbody/tr/td[2]/table[2]/tbody/tr/td/div/div"))
                .isDisplayed();
        for (int i = 0; nextBtn != false; i++) {
            try {
                Thread.sleep(1000);
                List<WebElement> anchorTags = driver.findElements(
                        By.xpath("//*[@id='classifiedsContainer']/table/tbody/tr/td[2]/table[1]//a"));

                for (WebElement anchorTag : anchorTags) {
                    htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
                    sBuffer.append(anchorTag.getAttribute("href"));
                    sBuffer.append("\n");
                }
                driver.findElement(By.xpath(
                        "//*[@id='classifiedsContainer']/table/tbody/tr/td[2]/table[2]/tbody/tr/td/div/div"))
                        .click();
                nextBtn = driver.findElement(By.xpath(
                        "//*[@id='classifiedsContainer']/table/tbody/tr/td[2]/table[2]/tbody/tr/td/div/div"))
                        .isDisplayed();
            } catch (Exception e) {
            }
        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);
    } else if (driver.getCurrentUrl().equals("http://www.wantaddigest.com/")) {
        // handling search field
        WebElement forSale = driver.findElement(By.xpath("//*[@id='country']"));
        forSale.sendKeys("guns");
        WebElement nextBtn = driver.findElement(By.xpath("//*[@id='left-home']/form/input[2]"));
        try {
            nextBtn.click();
            Thread.sleep(1000);
            List<WebElement> anchorTags = driver.findElements(By.xpath("//*[@id='middle']//a"));
            for (WebElement anchorTag : anchorTags) {
                htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
                sBuffer.append(anchorTag.getAttribute("href"));
                sBuffer.append("\n");
            }
        } catch (Exception e) {
        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);

    } else if (driver.getCurrentUrl().equals("http://www.nextechclassifieds.com/")) {
        // handling search field

        WebElement searchField = driver.findElement(By.xpath("//*[@id='nav-search-input']"));
        searchField.sendKeys("guns");
        WebElement searchBtn = driver.findElement(By.xpath("//*[@id='search-btn']"));
        searchBtn.click();
        boolean nextBtn = driver
                .findElement(By.xpath("//*[@id='site-wrapper']/div/div[2]/div/div[2]/ul[1]/li[8]/a"))
                .isEnabled();
        for (int i = 0; nextBtn != false; i++) {
            try {
                Thread.sleep(1000);
                List<WebElement> anchorTags = driver
                        .findElements(By.xpath("//*[@id='gallery-container']/ul//a"));
                for (WebElement anchorTag : anchorTags) {
                    htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
                    sBuffer.append(anchorTag.getAttribute("href"));
                    sBuffer.append("\n");
                }
                driver.findElement(By.xpath("//*[@id='site-wrapper']/div/div[2]/div/div[2]/ul[1]/li[8]/a"))
                        .click();
                nextBtn = driver
                        .findElement(By.xpath("//*[@id='site-wrapper']/div/div[2]/div/div[2]/ul[1]/li[8]/a"))
                        .isEnabled();
            } catch (Exception e) {
            }
        }
        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);
    } else if (driver.getCurrentUrl().equals("http://www.theoutdoorstrader.com/")) {
        // handling pagination and ajax interaction

        driver.manage().window().maximize();
        WebElement searchField = driver.findElement(By.xpath("//*[@id='QuickSearchQuery']"));
        searchField.click();
        searchField.sendKeys("guns");
        WebElement submitBtn = driver.findElement(By.xpath("//*[@id='QuickSearch']/form/div[2]/dl/dd/input"));
        submitBtn.click();
        List<WebElement> anchorTags = driver.findElements(By.xpath("//*[@id='content']/div/div//a"));
        for (WebElement anchorTag : anchorTags) {
            htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
            sBuffer.append(anchorTag.getAttribute("href"));
            sBuffer.append("\n");
        }

        boolean nextBtn = driver.findElement(By.linkText("Next >")).isDisplayed();

        for (int i = 0; nextBtn != false; i++) {
            try {
                Thread.sleep(1000);
                driver.findElement(By.linkText("Next >")).click();
                List<WebElement> anchors = driver.findElements(By.xpath("//*[@id='content']/div/div//a"));
                for (WebElement anchorTag : anchors) {
                    htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
                    sBuffer.append(anchorTag.getAttribute("href"));
                    sBuffer.append("\n");
                }
                nextBtn = driver.findElement(By.linkText("Next >")).isDisplayed();
            } catch (Exception e) {
                nextBtn = false;
            }
        }

        htmlpage += "</body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", htmlpage);
        System.out.println(htmlpage);
    } else if (driver.getCurrentUrl().equals("http://www.arguntrader.com/")) {
        // handling web form data

        driver.manage().window().maximize();
        WebElement searchField = driver
                .findElement(By.xpath("//*[@id='page-header']/div[2]/div/ul[2]/li[3]/a"));
        searchField.click();
        WebElement usernameField = driver.findElement(By.xpath("//*[@id='username']"));
        usernameField.sendKeys("testing");
        WebElement passwordField = driver.findElement(By.xpath("//*[@id='password']"));
        passwordField.sendKeys("usccsci572");
        WebElement loginBtn = driver
                .findElement(By.xpath("//*[@id='login']/div[1]/div/div/fieldset/dl[4]/dd/input[3]"));
        loginBtn.click();
        try {
            Thread.sleep(5000);
            List<WebElement> anchorTags = driver.findElements(By.xpath("//a"));

            for (WebElement anchorTag : anchorTags) {
                htmlpage += "<a href='" + anchorTag.getAttribute("href") + "'>test</a>";
                sBuffer.append(anchorTag.getAttribute("href"));
                sBuffer.append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("else loop");
    }
    writeURLToFile(sBuffer);
}

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

License:Apache License

public void processArmsList(WebDriver driver) {
    int agreeSize = 0;
    agreeSize = driver.findElements(By.xpath("/html//div[@class='ui-dialog-buttonset']/button[1]")).size();
    if (agreeSize > 0) {
        WebElement iAgree = driver.findElement(By.xpath("/html//div[@class='ui-dialog-buttonset']/button[1]"));
        iAgree.click();//from   w w  w. ja  v  a 2 s . c o m
    }
    int size = driver.findElements(By.xpath("html/body//div[@class='pager']//li[@class='next']/a")).size();
    if (size > 0) {
        String str1 = "<html><body><p>";
        while (size > 0) {
            List<WebElement> ele = driver.findElements(By.xpath("html/body//a"));
            for (WebElement we : ele) {
                str1 += "<a href='" + (String) we.getAttribute("href") + "'>" + (String) we.getAttribute("href")
                        + "</a><br>";
            }
            size = driver.findElements(By.xpath("html/body//div[@class='pager']//li[@class='next']/a")).size();
            if (size > 0) {
                driver.findElement(By.xpath("html/body//div[@class='pager']//li[@class='next']/a")).click();
            }
        }
        str1 += "</p></body></html>";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.innerHTML=arguments[0]", str1);
    }
}