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:GlennsPack.GlennWebAPI.TwitchBot.java

License:Open Source License

public TwitchBot(ArrayList<String> lookFor) {
    System.setProperty("webdriver.chrome.driver", "C:/Users/Glenn/Downloads/chromedriver/chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.manage().window().setSize(new Dimension(2000, 800));

    driver.manage().deleteAllCookies();/*from  w  w w  . jav a 2 s . c  o  m*/

    //      driver.get("http://twitch.tv/login");
    //
    //      driver.findElement(By.id("username")).sendKeys("kakansbot");
    //      driver.findElement(By.xpath("//*[@id=\"loginForm\"]/div[2]/input")).sendKeys("falcor1122");
    //      driver.findElement(By.xpath("//*[@id=\"loginForm\"]/div[3]/input")).click();
    //
    //      driver.get("http://www.twitch.tv/kakan9898/chat");

    driver.get("https://api.twitch.tv/kraken/oauth2/authorize"
            + "?response_type=token&client_id=ic1pa8ll9hazkssp35klwsmpelqejqj&redirect_uri=http://twitch.tv/kakan9898/chat&scope=chat_login");
    try {
        driver.findElement(By.xpath("//*[@id=\"header_login\"]/span")).click();
    } catch (Exception e) {
        // FIXME: handle exception
    }

    driver.findElement(By.xpath("//*[@id=\"login\"]")).sendKeys("kakansbot");
    driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("--");
    driver.findElement(By.xpath("//*[@id=\"oauth_submit\"]")).click();
    try {
        driver.findElement(By.xpath("//span[contains(text(),'Authorize')]")).click();
    } catch (Exception e) {
        // FIXME: handle exception
    }

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // FIXME Auto-generated catch block
        e.printStackTrace();
    }
    try {
        driver.findElement(By.xpath("//*[@id=\"noty_796318084299728000\"]/div/div[2]/svg")).click();
    } catch (Exception e) {
        // FIXME: handle exception
    }
    driver.findElement(By.id("ember673")).sendKeys("I am now monotoring this chat! Be aware!", Keys.ENTER);

    while (found != true) {
        try {
            for (int index = 0; index < lookFor.size(); index++) {
                List<WebElement> element = driver.findElements(
                        By.xpath("//*[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',\'" + abc
                                + "\'),\"" + lookFor.get(index).toLowerCase() + "\")]"));
                for (int i = 0; i < element.size(); i++) {
                    if (!(mathingsFound.contains(element.get(i)))) {
                        mathingsFound.add(element.get(i));
                        if (index < amounOfHello) {
                            sortOfText = "greet";
                            System.out.println(index + " <- index --- amountHello -> " + amounOfHello);
                        } else if (index >= amounOfHello && index < (amounOfHello + amountOfBye)) {
                            sortOfText = "bye";
                        } else {
                            sortOfText = "emoji";
                        }
                        System.out.println("FOUND " + lookFor.get(index) + "!");
                        System.err.println(element.get(i));
                        FoundString(lookFor.get(index).toLowerCase(), element.get(i).getText().toLowerCase(),
                                driver, sortOfText);
                    } else {
                        missmatch++;
                        if ((missmatch * lookFor.size()) == element.size()) {
                            System.out.println("OR ELSE");
                            throw new Exception();

                        }
                    }
                }
            }
            try {
                for (int i1 = 0; i1 < memesToLookFor.size(); i1++) {
                    List<WebElement> element = driver
                            .findElements(By.xpath("//img[@alt='" + memesToLookFor.get(i1) + "']"));
                    for (int i = 0; i < element.size(); i++) {
                        if (!(mathingsFound.contains(element.get(i)))) {
                            mathingsFound.add(element.get(i));
                            sortOfText = "meme";
                            FoundMeme(memesToLookFor.get(i1), driver);
                        }
                    }
                }
            } catch (Exception e) {
                // FIXME: handle exception
            }
        } catch (Exception e2) {
            e2.printStackTrace();
            System.out.println("Still running though");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // FIXME Auto-generated catch block
            e.printStackTrace();
            System.out.println("Still running though");
        }
    }
}

From source file:GlennsPack.GlennWebAPI.TwitchBot.java

License:Open Source License

public void FoundString(String textOfElement, String totalText, WebDriver driver, String sortOf) {

    if (totalText.length() > 1 && (((totalText.indexOf(textOfElement) > 0) && abc
            .indexOf(Character.toLowerCase(totalText.charAt((totalText.indexOf(textOfElement) - 1)))) == -1)
            || (totalText.indexOf(textOfElement) == 0))) {

        //Mste anvnda 6 istllet fr.size(), fr att bara ta kolla hlsningar
        if (sortOf == "greet") {
            for (int i = 0; i < amounOfHello; i++) {
                //            System.err.println("-"+stringsToLookFor.get(i).toLowerCase() + "- <--- stringsToLookFor -Lenght same? "+
                //                  (stringsToLookFor.get(i).length()==textOfElement.length())+"  - textofelement ---> -" + textOfElement.toLowerCase() + "-   True? " + 
                //                  (stringsToLookFor.get(i).toString().toLowerCase()==textOfElement.toString().toLowerCase()));
                if (textOfElement.length() == stringsToLookFor.get(i).length()) {
                    for (int i1 = 0; i1 < textOfElement.length(); i1++) {

                        if (textOfElement.toLowerCase().equals(stringsToLookFor.get(i).toLowerCase())) {

                            //Anvnder 6 hr ocks av samma anledning som For loopen tidigare ^
                            int rand = randFrase.nextInt(amounOfHello);
                            String send = stringsToLookFor.get(rand).toString() + "!";
                            driver.findElement(By.id("ember673")).sendKeys(send, Keys.ENTER);
                            charTrue = 0;
                            mathingsFound.addAll(driver.findElements(
                                    By.xpath("//*[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',\'"
                                            + abc + "\'),\"" + send.toLowerCase() + "\")]")));
                        }//from  w w  w.  j a  v  a2  s. co  m
                    }
                }
            }
        } else if (sortOf == "bye") {
            for (int i = amounOfHello; i < (amounOfHello + amountOfBye); i++) {
                //            System.err.println("-"+stringsToLookFor.get(i).toLowerCase() + "- <--- stringsToLookFor -Lenght same? "+
                //                  (stringsToLookFor.get(i).length()==textOfElement.length())+"  - textofelement ---> -" + textOfElement.toLowerCase() + "-   True? " + 
                //                  (stringsToLookFor.get(i).toString().toLowerCase()==textOfElement.toString().toLowerCase()));
                if (textOfElement.length() == stringsToLookFor.get(i).length()) {
                    for (int i1 = 0; i1 < textOfElement.length(); i1++) {

                        if (textOfElement.toLowerCase().equals(stringsToLookFor.get(i).toLowerCase())) {
                            charTrue++;
                        }
                        if (charTrue == textOfElement.length()) {
                            int rand = randFrase.nextInt(amountOfBye);
                            String send = stringsToLookFor.get(amounOfHello + rand).toString() + "!";
                            driver.findElement(By.id("ember673")).sendKeys(send, Keys.ENTER);
                            charTrue = 0;
                            mathingsFound.addAll(driver.findElements(
                                    By.xpath("//*[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',\'"
                                            + abc + "\'),\"" + send.toLowerCase() + "\")]")));
                        }
                    }
                }
            }
        } else if (sortOf == "emoji") {
            System.out.println(textOfElement + " = text of element --- same? "
                    + (textOfElement.equals("love".toString())));
            if (textOfElement.equals("love")) {
                send = "<3";
            } else if (textOfElement.equals("cool")) {
                send = "B)";
            }
            System.out.println(send);
            driver.findElement(By.id("ember673")).sendKeys(send, Keys.ENTER);
            mathingsFound.addAll(driver
                    .findElements(By.xpath("//*[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',\'"
                            + abc + "\'),\"" + send.toLowerCase() + "\")]")));
            mathingsFound.addAll(driver.findElements(By.xpath("//img[@alt='" + send + "']")));
        }
        charTrue = 0;
    }
}

From source file:GlennsPack.GlennWebAPI.TwitchBot.java

License:Open Source License

public void FoundMeme(String meme, WebDriver driver) {
    charTrue = 0;/*from www .j  a  v  a  2s.  c o  m*/
    send = meme;
    driver.findElement(By.id("ember673")).sendKeys(send, Keys.ENTER);
    charTrue = 0;
    mathingsFound.addAll(driver.findElements(By.xpath("//img[@alt='" + meme + "']")));
}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.login.LoginHandler26.java

License:Apache License

public void processDriver(WebDriver driver) {

    try {//from  w  w w.  j ava  2 s .co m
        String accumulatedData = "";

        driver.findElement(By.tagName("body")).getAttribute("innerHTML");
        Configuration conf = NutchConfiguration.create();
        new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));

        List<WebElement> atags = driver.findElements(By.tagName("a"));
        int numberofajaxlinks = atags.size();
        for (int i = 0; i < numberofajaxlinks; i++) {

            if (atags.get(i).getAttribute("href") != null
                    && atags.get(i).getAttribute("href").equals("javascript:void(null);")) {

                atags.get(i).click();

                if (i == numberofajaxlinks - 1) {
                    // append everything to the driver in the last round
                    JavascriptExecutor jsx = (JavascriptExecutor) driver;
                    jsx.executeScript(
                            "document.body.innerHTML=document.body.innerHTML " + accumulatedData + ";");
                    continue;
                }

                accumulatedData += driver.findElement(By.tagName("body")).getAttribute("innerHTML");

                // refreshing the handlers as the page was interacted with
                driver.navigate().refresh();
                new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));
                atags = driver.findElements(By.tagName("a"));
            }
        }
    } catch (Exception e) {
        LOG.info(StringUtils.stringifyException(e));
    }
}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.login.LoginHandlerUK3.java

License:Apache License

public boolean shouldProcessURL(String URL) {

    WebDriver driver = new FirefoxDriver();
    // to resolve selinum issue with Firefox
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    try {/*w ww.  j  a v a  2  s  .c  om*/
        Thread.sleep(10000);
    } catch (Exception e) {
        Http.LOG.error("error in try dishant " + e);
    }

    Http.LOG.error("trigger" + URL);
    if (URL.equals("http://www.arguntrader.com")) {
        Http.LOG.error("in" + URL);
        // login for arguntrader.com
        driver.get("http://www.arguntrader.com/ucp.php?mode=login");
        WebElement query = driver.findElement(By.id("username"));
        query.sendKeys("testtest");
        query = driver.findElement(By.id("password"));
        query.sendKeys("password123");
        driver.findElement(By.name("login")).click();
    } else if (URL.equals("http://www.iguntrade.com")) {
        Http.LOG.error("in" + URL);
        // pagination for iguntrade.com
        driver.get("http://www.iguntrade.com/index.php?page=search");
        List<WebElement> allpages = driver.findElements(By.xpath("//div[@class='paginate']//a"));
        for (int i = 0; i <= (allpages.size()); i++) {
            allpages.get(i).click();

        }
    } else if (URL.contains("http://www.zidaho.com/category")) {
        Http.LOG.error("in" + URL);
        // pagination for zidaho.com
        List<WebElement> allpages = driver
                .findElements(By.xpath("//ul[@class='pagination browsing_result_page_links']//a"));
        for (int i = 0; i <= (allpages.size()); i++) {
            allpages.get(i).click();

        }
    }

    return true;
}

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
 *///www  .  j  av  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.MultiplePatterns.CS572LoginAndClickHandler.java

License:Apache License

private void clickAllAjaxLinks(Configuration conf, WebDriver driver) {
    int count = 0;
    new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));
    List<WebElement> atags = driver.findElements(By.tagName("a"));
    int numberofajaxlinks = atags.size();
    for (int i = 0; i < numberofajaxlinks; i++) {

        if ((atags.get(i).getAttribute("href") != null)
                && (atags.get(i).getAttribute("href").contains("javascript:void")
                        || atags.get(i).getAttribute("href").equals("javascript:;")
                        || (atags.get(i).getAttribute("onclick") != null
                                && atags.get(i).getAttribute("onclick").contains("return false")))

        ) {//from w  w  w. j a va2  s  .  c o m

            //         System.out.println(atags.get(i).getAttribute("onClick"));
            try {
                atags.get(i).click();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                count++;
                recordHtmlBodyWithDynamicChange(driver);
            } catch (Exception e) {
                LOG.info(StringUtils.stringifyException(e));
                continue;
            }
        }

        if ((i == numberofajaxlinks - 1) || (count > 18)) { //Don't over-click
            JavascriptExecutor jsx = (JavascriptExecutor) driver;
            Iterator<String> itr = accumulatedDataArrayList.iterator();
            while (itr.hasNext()) {
                String accumulatedData = itr.next();
                try {
                    jsx.executeScript("document.body.innerHTML = document.body.innerHTML + " + "'"
                            + accumulatedData + "'" + ";");
                } catch (Exception ee) {
                    System.out.println(ee);
                }
            }
            accumulatedDataArrayList.clear();
            break;
        }

        if (refreshFlag) {
            refreshFlag = false;
            // refresh page to refresh handler to the un-touched state
            driver.navigate().refresh();
            new WebDriverWait(driver, conf.getLong("libselenium.page.load.delay", 3));
            atags = driver.findElements(By.tagName("a"));
        }

    }
    //  System.exit(0);

}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.PageNavigation.PageNavigationUK1.java

License:Apache License

public void processDriverForGandermountain(WebDriver driver) {
    List<WebElement> search = driver.findElements(By.className("top-level-link"));
    for (WebElement element : search) {
        String link = element.getAttribute("href");
        if (link.contains("guns")) {
            driver.get(link);//from  w ww.  j ava  2s  .co m
            return;
        }
    }
}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.PageNavigation.PageNavigationUK1.java

License:Apache License

public void processDriverForIwanna(WebDriver driver) {
    String data = "";
    String nextLink = "";
    String[] keywords = { "rifle", "hawk", "gun", "pistol", "firearm", "grenade", "bomb", "sniper", "sword",
            "knife", "knives", "flamethrower", "carbine", "revolver", "missile", "barrel", "bullet",
            "gunpowder", "muzzle", "trigger", "weapon", "ammo", "ammunition" };

    List<WebElement> elements = driver.findElement(By.id("tag_cloud")).findElements(By.tagName("a"));
    for (WebElement element : elements) {
        String link = element.getAttribute("href");
        LOOP: for (String key : keywords) {
            if (link.contains(key)) {
                WebDriver travel = new FirefoxDriver();
                travel.get(link);/*  w w w .  ja  v a  2  s .  c om*/
                while (true) {
                    try {
                        List<WebElement> listings = travel.findElements(By.className("listing"));
                        for (WebElement listing : listings)
                            data += listing.findElement(By.className("column70")).findElement(By.tagName("a"))
                                    .getAttribute("href") + " ";
                        JavascriptExecutor executor = (JavascriptExecutor) travel;
                        nextLink = travel.findElement(By.className("next")).getAttribute("onclick") + ";";
                        executor.executeScript(nextLink);
                    } catch (Exception e) {
                        travel.quit();
                        break LOOP;
                    }
                }
            }
        }
    }
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("document.body.innerHTML=document.body.innerHTML + \"" + data + "\";");

}

From source file:gov.nasa.jpl.memex.nutch.protocol.selenium.handlers.PageNavigation.PageNavigationUK1.java

License:Apache License

public void processDriverForLionseek(WebDriver driver) {
    String data = "";
    boolean flag = false;
    String[] keywords = { "rifle", "hawk", "gun", "pistol", "firearm", "grenade", "bomb", "sniper", "sword",
            "knife", "knives", "flamethrower", "carbine", "revolver", "missile", "barrel", "bullet",
            "gunpowder", "muzzle", "trigger", "weapon", "ammo", "ammunition" };

    List<WebElement> elements = driver.findElements(By.className("category"));
    for (WebElement element : elements) {
        flag = false;/* w  w  w.j ava 2 s.c om*/
        String alt = element.findElement(By.tagName("img")).getAttribute("alt").toLowerCase();
        LOOP: for (String key : keywords) {
            if (alt.contains(key)) {
                data += element.getAttribute("href") + " ";
                flag = true;
                break LOOP;
            }
        }
        if (!flag) {
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("document.querySelector('a[title=\"" + element.getAttribute("title")
                    + "\"]').setAttribute(\"href\", \"#\");");
        }

    }
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("document.querySelector('a[href=\"/sitemap\"]').setAttribute(\"href\", \"#\");");
    executor.executeScript("document.body.innerHTML=document.body.innerHTML + \"" + data + "\";");
}