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:edu.uga.cs.clickminer.util.FrameUtils.java

License:Open Source License

/**
 * Gets the child frames.//w ww  .j a va2 s. c  om
 *
 * @return the child frames
 * @param wdriver a {@link org.openqa.selenium.WebDriver} object.
 */
public static List<WebElement> getChildFrames(WebDriver wdriver) {
    List<WebElement> retval = new ArrayList<WebElement>();
    try {
        retval.addAll(wdriver.findElements(By.tagName("frame")));
    } catch (NoSuchElementException e) {
    }
    try {
        retval.addAll(wdriver.findElements(By.tagName("iframe")));
    } catch (NoSuchElementException e) {
    }
    if (log.isDebugEnabled()) {
        log.debug("Located " + retval.size() + " child frames.");
    }
    return retval;
}

From source file:edu.usc.cs.ir.selenium.handler.Glocktalk.java

License:Apache License

private Set<String> getThreadsFromForum(WebDriver driver) {
    Set<String> threads = new HashSet<String>();
    List<WebElement> threadTopics = driver.findElements(By.xpath("//h3[@class='title']"));

    for (WebElement element : threadTopics) {
        WebElement anchor = element.findElement(By.tagName("a"));
        //System.out.println(anchor.getAttribute("href"));
        threads.add("<a href=\"" + anchor.getAttribute("href") + "\" />");
    }/*from w w  w  . j  a va 2  s .  c om*/
    return threads;
}

From source file:edu.usc.cs.ir.selenium.handler.Glocktalk.java

License:Apache License

private Set<String> getPostsFromThread(WebDriver driver) {
    List<WebElement> links = driver.findElements(By.xpath("//ol[@id='messageList']//*[@href|@src]"));
    Set<String> filteredLinks = new HashSet<String>();
    for (WebElement link : links) {
        String linkValue = null;/*from   w  ww.j  a  v a  2s  .  co m*/
        if (link.getAttribute("src") != null && !link.getTagName().equals("img"))
            continue;
        if (link.getAttribute("src") != null)
            linkValue = link.getAttribute("src");
        else
            linkValue = link.getAttribute("href");
        if (linkValue.startsWith("http://www.glocktalk.com/account")
                || linkValue.startsWith("http://www.glocktalk.com/misc")
                || linkValue.startsWith("http://www.glocktalk.com/goto")
                || linkValue.startsWith("http://www.glocktalk.com/social-forums")
                || linkValue.startsWith("http://www.glocktalk.com/search") || linkValue.contains("#"))
            continue;
        //System.out.println(linkValue);
        filteredLinks.add("<a href=\"" + linkValue + "\" />");
    }
    return filteredLinks;
}

From source file:edu.usc.cs.ir.selenium.handler.Glocktalk.java

License:Apache License

private Set<String> getImagesFromMedia(WebDriver driver) {
    List<WebElement> srcLinks = driver.findElements(
            By.xpath("//div[@class='gridSection gridGroup LightboxContainer LightboxLoadable']//*[@src]"));
    Set<String> images = new HashSet<String>();
    for (WebElement link : srcLinks) {
        String linkValue = null;/*from   w w  w.j a va 2s. c  om*/
        if (!link.getTagName().equals("img"))
            continue;
        linkValue = link.getAttribute("src");
        System.out.println(linkValue);
        images.add("<img src=\"" + linkValue + "\" />");
    }
    return images;
}

From source file:edu.usc.cs.ir.selenium.handler.Glocktalk.java

License:Apache License

public String processDriver(WebDriver driver) {
    StringBuffer buffer = new StringBuffer();

    // Extract content - getText doesn't return any links
    String content = driver.findElement(By.tagName("body")).getText();
    buffer.append(content).append("\n");

    // Extract threads from a forum through all of its navigation
    if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/forum")) {
        // Extract the last page number
        List<WebElement> pages = driver.findElements(By.xpath("//div[@class='PageNav']//nav//a[@class='']"));
        buffer.append(getThreadsFromForum(driver)).append("\n");

        if (pages != null && !pages.isEmpty()) {

            WebElement currentPage = driver
                    .findElement(By.xpath("//div[@class='PageNav']//nav//a[@class='currentPage ']"));
            Integer currentPageNo = Integer.parseInt(currentPage.getText());
            if (currentPageNo % PAGE_FETCH_LIMIT == 1) {
                Integer lastPage = Integer.parseInt(pages.get(pages.size() - 1).getText());
                String currentUrl = driver.getCurrentUrl().substring(0,
                        driver.getCurrentUrl().lastIndexOf("/") + 1);
                int i = currentPageNo + 1;
                for (; i < (currentPageNo + PAGE_FETCH_LIMIT) && i <= lastPage; i++) {
                    try {
                        driver.get(currentUrl + "page-" + i);
                    } catch (TimeoutException e) {
                        System.out.println("Timeout Exception for page = " + i);
                    } finally {
                        buffer.append(getThreadsFromForum(driver)).append("\n");
                    }//w  w  w  .  j a v a2s  .c  o  m
                }

                if (i <= lastPage)
                    buffer.append("<a href=\"").append(currentUrl + "page-" + i).append("\" />").append("\n");
            }
        }
    }

    // Extract all links from a thread
    else if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/threads")) {

        // Extract the last page number
        List<WebElement> pages = driver.findElements(By.xpath("//div[@class='PageNav']//nav//a[@class='']"));
        buffer.append(getPostsFromThread(driver)).append("\n");

        if (pages != null && !pages.isEmpty()) {

            WebElement currentPage = driver
                    .findElement(By.xpath("//div[@class='PageNav']//nav//a[@class='currentPage ']"));
            Integer currentPageNo = Integer.parseInt(currentPage.getText());
            if (currentPageNo % PAGE_FETCH_LIMIT == 1) {
                Integer lastPage = Integer.parseInt(pages.get(pages.size() - 1).getText());
                String currentUrl = driver.getCurrentUrl().substring(0,
                        driver.getCurrentUrl().lastIndexOf("/") + 1);
                int i = currentPageNo + 1;
                for (; i < (currentPageNo + PAGE_FETCH_LIMIT) && i <= lastPage; i++) {
                    try {
                        driver.get(currentUrl + "page-" + i);
                    } catch (TimeoutException e) {
                        System.out.println("Timeout Exception for page = " + i);
                    } finally {
                        buffer.append(getPostsFromThread(driver)).append("\n");
                    }
                }

                if (i <= lastPage)
                    buffer.append("<a href=\"").append(currentUrl + "page-" + i).append("\" />").append("\n");
            }
        }

    }

    // Extract User Images
    else if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/members")) {
        List<WebElement> srcLinks = driver.findElements(By.xpath("//div[@class='profilePage']//*[@src]"));
        Set<String> images = new HashSet<String>();
        for (WebElement link : srcLinks) {
            String linkValue = null;
            if (!link.getTagName().equals("img"))
                continue;
            linkValue = link.getAttribute("src");
            System.out.println(linkValue);
            images.add("<img src=\"" + linkValue + "\" />");
        }
        buffer.append(images);
    }

    // Extract Media - Images
    else if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/media")) {

        // Extract the last page number
        List<WebElement> pages = driver.findElements(By.xpath("//div[@class='PageNav']//nav//a[@class='']"));
        buffer.append(getImagesFromMedia(driver)).append("\n");

        if (pages != null && !pages.isEmpty()) {

            WebElement currentPage = driver
                    .findElement(By.xpath("//div[@class='PageNav']//nav//a[@class='currentPage ']"));
            Integer currentPageNo = Integer.parseInt(currentPage.getText());
            if (currentPageNo % PAGE_FETCH_LIMIT == 1) {
                Integer lastPage = Integer.parseInt(pages.get(pages.size() - 1).getText());
                String currentUrl = driver.getCurrentUrl().substring(0,
                        driver.getCurrentUrl().lastIndexOf("/"));
                int i = currentPageNo + 1;
                for (; i < (currentPageNo + PAGE_FETCH_LIMIT) && i <= lastPage; i++) {
                    try {
                        driver.get(currentUrl + "?page=" + i);
                    } catch (TimeoutException e) {
                        System.out.println("Timeout Exception for page = " + i);
                    } finally {
                        buffer.append(getImagesFromMedia(driver)).append("\n");
                    }
                }

                if (i <= lastPage)
                    buffer.append("<a href=\"").append(currentUrl + "?page=" + i).append("\" />").append("\n");
            }
        }
    }

    //System.out.println();
    return buffer.toString();
}

From source file:edu.usc.cs.ir.selenium.handler.GlocktalkBasic.java

License:Apache License

public String processDriver(WebDriver driver) {
    StringBuffer buffer = new StringBuffer();

    // Extract content - getText doesn't return any links
    String content = driver.findElement(By.tagName("body")).getText();
    buffer.append(content).append("\n");

    // Extract threads from a forum
    if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/forum")) {
        List<WebElement> threadTopics = driver.findElements(By.xpath("//h3[@class='title']"));

        for (WebElement element : threadTopics) {
            WebElement anchor = element.findElement(By.tagName("a"));
            //System.out.println(anchor.getAttribute("href"));
            buffer.append("<a href=\"").append(anchor.getAttribute("href")).append("\" />").append("\n");
        }/*  w  w  w  .  j a  va 2  s .c  o  m*/

    }

    // Extract Next Page Link
    List<WebElement> nav = driver.findElements(By.xpath("//div[@class='PageNav']//nav//*[@class='text']"));
    //System.out.println(nav.get(nav.size() - 1).getAttribute("href"));
    if (nav.size() > 0)
        buffer.append("<a href=\"").append(nav.get(nav.size() - 1).getAttribute("href")).append("\" />")
                .append("\n");

    // Extract all links from a thread
    if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/threads")) {
        List<WebElement> links = driver.findElements(By.xpath("//ol[@id='messageList']//*[@href]"));
        Set<String> filteredLinks = new HashSet<String>();
        for (WebElement link : links) {
            String linkValue = "<a href=\"" + link.getAttribute("href") + "\" />";
            if (linkValue.startsWith("http://www.glocktalk.com/account")
                    || linkValue.startsWith("http://www.glocktalk.com/misc")
                    || linkValue.startsWith("http://www.glocktalk.com/goto")
                    || linkValue.startsWith("http://www.glocktalk.com/social-forums")
                    || linkValue.startsWith("http://www.glocktalk.com/search") || linkValue.contains("#"))
                continue;
            //System.out.println(linkValue);
            filteredLinks.add(linkValue);
        }
        if (filteredLinks.size() > 0)
            buffer.append(filteredLinks).append("\n");
    }
    //System.out.println();
    return buffer.toString();
}

From source file:endtoend.browser.util.JsOnOffTestUtils.java

License:Apache License

public static void assertJavaScriptIsOn(WebDriver driver) {
    driver.get(classNameToTestFileUrl(JsOnOffTestUtils.class));
    assertThat(driver.findElements(By.tagName("div")), hasSize(1 + 3));
}

From source file:endtoend.browser.util.JsOnOffTestUtils.java

License:Apache License

public static void assertJavaScriptIsOff(WebDriver driver) {
    driver.get(classNameToTestFileUrl(JsOnOffTestUtils.class));
    assertThat(driver.findElements(By.tagName("div")), hasSize(1));
}

From source file:es.urjc.etsii.code.WebRtcBenchmarkTest.java

License:Apache License

@Before
public void setup() {
    // Set defaults in all browsers
    int sessionsNumber = getProperty(SESSIONS_NUMBER_PROP, SESSIONS_NUMBER_DEFAULT);
    int initSessionNumber = getProperty(INIT_SESSION_NUMBER_PROP, INIT_SESSION_NUMBER_DEFAULT);
    for (int i = 0; i < sessionsNumber; i++) {
        WebDriver[] webDrivers = { getPresenter(i).getBrowser().getWebDriver(),
                getViewer(i).getBrowser().getWebDriver() };

        for (WebDriver webDriver : webDrivers) {
            // Session number
            WebElement sessionNumberWe = webDriver.findElement(By.id("sessionNumber"));
            sessionNumberWe.clear();/*  www  .ja v  a 2s .co  m*/
            sessionNumberWe.sendKeys(String.valueOf(i + initSessionNumber));

            // Media processing
            String processing = getProperty(MEDIA_PROCESSING_PROP, MEDIA_PROCESSING_DEFAULT);
            Select processingSelect = new Select(webDriver.findElement(By.id("processing")));
            processingSelect.selectByValue(processing);

            // Bandwidth
            String bandwidth = String.valueOf(getProperty(BANDWIDTH_PROP, BANDWIDTH_DEFAULT));
            WebElement bandwidthWe = webDriver.findElement(By.id("bandwidth"));
            bandwidthWe.clear();
            bandwidthWe.sendKeys(bandwidth);

            // Number of fake clients
            int fakeClientsInt = getProperty(FAKE_CLIENTS_NUMBER_PROP, FAKE_CLIENTS_NUMBER_DEFAULT);
            String fakeClients = String.valueOf(fakeClientsInt);
            WebElement fakeClientsWe = webDriver.findElement(By.id("fakeClients"));
            fakeClientsWe.clear();
            fakeClientsWe.sendKeys(fakeClients);

            // Rate between clients (milliseconds)
            int timeBetweenClients = getProperty(FAKE_CLIENTS_RATE_PROP, FAKE_CLIENTS_RATE_DEFAULT);
            WebElement timeBetweenClientsWe = webDriver.findElement(By.id("timeBetweenClients"));
            timeBetweenClientsWe.clear();
            timeBetweenClientsWe.sendKeys(String.valueOf(timeBetweenClients));

            if (fakeClientsInt > 0) {
                extraTimePerFakeClients = fakeClientsInt * timeBetweenClients / 1000;
            }

            // Remove fake clients
            boolean removeFakeClients = getProperty(FAKE_CLIENTS_REMOVE_PROP, FAKE_CLIENTS_REMOVE_DEFAULT);
            List<WebElement> removeFakeClientsList = webDriver.findElements(By.name("removeFakeClients"));
            removeFakeClientsList.get(removeFakeClients ? 0 : 1).click();

            // Time with all fake clients together (seconds)
            if (removeFakeClients) {
                int playTime = getProperty(FAKE_CLIENTS_TOGETHER_TIME_PROP, FAKE_CLIENTS_TOGETHER_TIME_DEFAULT);
                WebElement playTimeWe = webDriver.findElement(By.id("playTime"));
                playTimeWe.clear();
                playTimeWe.sendKeys(String.valueOf(playTime));

                extraTimePerFakeClients = (extraTimePerFakeClients * 2) + playTime;
            }

            // Number of fake clients per KMS instance
            String fakeClientsPerInstance = String
                    .valueOf(getProperty(FAKE_CLIENTS_PER_KMS_PROP, FAKE_CLIENTS_PER_KMS_DEFAULT));
            WebElement fakeClientsPerInstanceWe = webDriver.findElement(By.id("fakeClientsPerInstance"));
            fakeClientsPerInstanceWe.clear();
            fakeClientsPerInstanceWe.sendKeys(fakeClientsPerInstance);

        }
    }

    if (!outputFolder.endsWith(File.separator)) {
        outputFolder += File.separator;
    }
}

From source file:GlennsPack.GlennWebAPI.findTextInPage.java

License:Open Source License

public findTextInPage(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.get("http://gojb.ml/chat");

    try {//from  www. j a  v  a  2 s.c  o  m
        Thread.sleep(500);
    } catch (InterruptedException e1) {
        // FIXME Auto-generated catch block
        e1.printStackTrace();
    }
    driver.findElement(By.xpath("//*[@id=\"menu\"]/header/div[1]")).click();
    driver.findElement(By.id("knapp")).click();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // FIXME Auto-generated catch block
        e.printStackTrace();
    }
    driver.switchTo().alert().sendKeys("GoJbBot");
    driver.switchTo().alert().accept();
    driver.findElement(By.id("knapp2")).click();

    while (found != true) {
        try {
            for (int index = 0; index < lookFor.size(); index++) {
                List<WebElement> element = driver
                        .findElements(By.xpath("//*[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',"
                                + "'abcdefghijklmnopqrstuvwxyz'),\"" + lookFor.get(index).toLowerCase()
                                + "\")]"));

                for (int i = 0; i < element.size(); i++) {

                    if (!mathingsFound.contains(element.get(i).getText())) {
                        mathingsFound.add(element.get(i).getText());
                        System.out.println("FOUND!");
                        Found(lookFor.get(index).toLowerCase(), element.get(i).getText(), driver);
                    } else {
                        missmatch++;
                        if ((missmatch * lookFor.size()) == element.size()) {
                            System.out.println("OR ELSE");
                            throw new Exception();
                        }
                    }
                }
            }
        } catch (Exception e2) {
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // FIXME Auto-generated catch block
            e.printStackTrace();
        }
    }

}