Example usage for org.openqa.selenium.remote RemoteWebDriver findElementsByTagName

List of usage examples for org.openqa.selenium.remote RemoteWebDriver findElementsByTagName

Introduction

In this page you can find the example usage for org.openqa.selenium.remote RemoteWebDriver findElementsByTagName.

Prototype

@Override
    public List<WebElement> findElementsByTagName(String using) 

Source Link

Usage

From source file:com.screenslicer.core.scrape.QueryCommon.java

License:Open Source License

public static boolean doAuth(RemoteWebDriver driver, Credentials credentials) throws ActionFailed {
    if (credentials == null || CommonUtil.isEmpty(credentials.username)
            || CommonUtil.isEmpty(credentials.password)) {
        return false;
    }//from   www. java 2  s .c  o  m
    try {
        List<WebElement> inputs = driver.findElementsByTagName("input");
        String html = CommonUtil.strip(driver.findElementByTagName("body").getAttribute("outerHTML"), true);
        List<WebElement> usernames = new ArrayList<WebElement>();
        List<WebElement> passwords = new ArrayList<WebElement>();
        List<String> usernamesHtml = new ArrayList<String>();
        List<String> passwordsHtml = new ArrayList<String>();
        for (WebElement input : inputs) {
            String type = input.getAttribute("type");
            if ("text".equalsIgnoreCase(type)) {
                usernames.add(input);
                String controlHtml = input.getAttribute("outerHTML");
                controlHtml = CommonUtil.strip(controlHtml, true);
                usernamesHtml.add(controlHtml);
            } else if ("password".equalsIgnoreCase(type)) {
                passwords.add(input);
                String controlHtml = input.getAttribute("outerHTML");
                controlHtml = CommonUtil.strip(controlHtml, true);
                passwordsHtml.add(controlHtml);
            }
        }
        class Login {
            final WebElement username;
            final WebElement password;
            final int index;

            Login(WebElement username, WebElement password, int index) {
                this.username = username;
                this.password = password;
                this.index = index;
            }
        }
        ;
        List<Login> logins = new ArrayList<Login>();
        for (int curPassword = 0; curPassword < passwords.size(); curPassword++) {
            int passwordIndex = html.indexOf(passwordsHtml.get(curPassword));
            int minDist = Integer.MAX_VALUE;
            int indexOfMin = -1;
            WebElement minUsername = null;
            WebElement minPassword = passwords.get(curPassword);
            for (int curUsername = 0; curUsername < usernames.size(); curUsername++) {
                int usernameIndex = html.indexOf(usernamesHtml.get(curUsername));
                if (usernameIndex < passwordIndex && passwordIndex - usernameIndex < minDist
                        && usernameIndex > -1 && passwordIndex > -1) {
                    minDist = passwordIndex - usernameIndex;
                    minUsername = usernames.get(curUsername);
                    indexOfMin = (usernameIndex + passwordIndex) / 2;
                }
            }
            logins.add(new Login(minUsername, minPassword, indexOfMin));
        }
        if (!logins.isEmpty()) {
            Login closestLogin = logins.get(0);
            if (logins.size() > 1) {
                //TODO translate
                Pattern hints = Pattern.compile(
                        "(?:log(?:ged)?\\s?-?in)|(?:sign(?:ed)?\\s?\\-?in)|(?:remember\\s?me)|(?:tabindex\\s?=\\s?[^0-9]?[12][^0-9])",
                        Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);
                Matcher matcher = hints.matcher(html);
                int closest = Integer.MAX_VALUE;
                while (matcher.find()) {
                    int start = matcher.start();
                    for (Login login : logins) {
                        int dist = Math.abs(login.index - start);
                        if (dist < closest) {
                            closest = dist;
                            closestLogin = login;
                        }
                    }
                }
            }
            QueryCommon.typeText(driver, closestLogin.username, credentials.username, true, false);
            QueryCommon.typeText(driver, closestLogin.password, credentials.password, false, true);
            return true;
        }
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
    throw new ActionFailed("Could not sign in");
}

From source file:com.screenslicer.core.scrape.QueryForm.java

License:Open Source License

private static void loadLabels(RemoteWebDriver driver, List<HtmlNode> controls) throws ActionFailed {
    try {/* ww  w . j  av  a 2s.co m*/
        List<WebElement> labels = driver.findElementsByTagName("label");
        Map<String, String> labelMap = new HashMap<String, String>();
        for (WebElement label : labels) {
            String labelFor = label.getAttribute("for");
            String labelText = label.getText();
            if (!CommonUtil.isEmpty(labelFor) && !CommonUtil.isEmpty(labelText)) {
                labelMap.put(labelFor.trim(), labelText.trim());
            }
        }
        for (HtmlNode control : controls) {
            if (!CommonUtil.isEmpty(control.id)) {
                control.label = labelMap.get(control.id);
            }
        }
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.screenslicer.core.scrape.QueryKeyword.java

License:Open Source License

private static List<WebElement> findSearchBox(RemoteWebDriver driver, boolean strict) throws ActionFailed {
    try {/*from  w  w w. j  av a 2s . c om*/
        List<WebElement> searchBoxes = new ArrayList<WebElement>();
        List<WebElement> allInputs = driver.findElementsByTagName("input");
        for (WebElement searchBox : allInputs) {
            if (searchBox.getAttribute("type").equalsIgnoreCase("text")
                    || searchBox.getAttribute("type").equalsIgnoreCase("search")) {
                searchBoxes.add(searchBox);
            }
        }

        List<WebElement> prioritySearchBoxes = new ArrayList<WebElement>();
        List<WebElement> forbiddenSearchBoxes = new ArrayList<WebElement>();
        for (WebElement searchBox : searchBoxes) {
            String info = new String(searchBox.getAttribute("name") + " " + searchBox.getAttribute("title")
                    + " " + searchBox.getAttribute("value") + " " + searchBox.getAttribute("class") + " "
                    + searchBox.getAttribute("placeholder") + " " + searchBox.getAttribute("id"));
            if (strict && nonSearch.matcher(info).find()) {
                forbiddenSearchBoxes.add(searchBox);
            } else if (search.matcher(info).find()) {
                prioritySearchBoxes.add(0, searchBox);
            }
        }
        for (WebElement searchBox : forbiddenSearchBoxes) {
            searchBoxes.remove(searchBox);
        }
        for (WebElement searchBox : prioritySearchBoxes) {
            searchBoxes.remove(searchBox);
            searchBoxes.add(0, searchBox);
        }
        if (!strict && searchBoxes.isEmpty() && allInputs.size() == 1) {
            searchBoxes.add(allInputs.get(0));
        } else if (!strict && searchBoxes.isEmpty() && prioritySearchBoxes.size() == 1) {
            searchBoxes.add(prioritySearchBoxes.get(0));
        }
        return searchBoxes;
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.screenslicer.core.scrape.QueryKeyword.java

License:Open Source License

private static List<WebElement> navigateToSearch(RemoteWebDriver driver, String tagName, String type,
        boolean strict) throws ActionFailed {
    try {//from  ww w  . j  a va  2  s. c o  m
        List<WebElement> tags = driver.findElementsByTagName(tagName);
        boolean success = false;
        for (WebElement tag : tags) {
            try {
                if (type == null || type.equalsIgnoreCase(tag.getAttribute("type"))) {
                    String info = new String(tag.getAttribute("href") + " " + tag.getText());
                    if (searchControl.matcher(info).find()) {
                        hoverClick(driver, tag);
                        success = true;
                        break;
                    }
                }
            } catch (Exception e) {
                Log.exception(e);
            }
        }
        if (!success) {
            for (WebElement tag : tags) {
                try {
                    if (type == null || type.equalsIgnoreCase(tag.getAttribute("type"))) {
                        String extendedInfo = new String(
                                tag.getAttribute("name") + " " + tag.getAttribute("title") + " "
                                        + tag.getAttribute("class") + " " + tag.getAttribute("id"));
                        if (searchControl.matcher(extendedInfo).find()) {
                            hoverClick(driver, tag);
                            success = true;
                            break;
                        }
                    }
                } catch (Exception e) {
                    Log.exception(e);
                }
            }
        }
        if (success) {
            return findSearchBox(driver, strict);
        }
        return new ArrayList<WebElement>();
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.screenslicer.core.scrape.QueryKeyword.java

License:Open Source License

private static void handleIframe(RemoteWebDriver driver) throws ActionFailed {
    List<WebElement> iframes = null;
    try {/*from ww  w. j  a v  a 2  s . com*/
        iframes = driver.findElementsByTagName("iframe");
    } catch (Throwable t) {
        throw new ActionFailed(t);
    }
    try {
        for (WebElement iframe : iframes) {
            try {
                Dimension dim = iframe.getSize();
                if (iframe.isDisplayed() && (dim.getHeight() * dim.getWidth()) > MIN_IFRAME_AREA) {
                    String src = iframe.getAttribute("src");
                    if (!CommonUtil.isEmpty(src) && src.indexOf("://") > -1 && src.indexOf("?") > -1) {
                        String origHandle = null;
                        String origUrl = null;
                        String newHandle = null;
                        try {
                            origHandle = driver.getWindowHandle();
                            origUrl = driver.getCurrentUrl();
                            newHandle = Util.newWindow(driver);
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                        boolean undo = false;
                        try {
                            Util.get(driver, src, true);
                            driver.executeScript(
                                    "document.getElementsByTagName('html')[0].style.overflow='scroll';");
                            Util.driverSleepShort();
                            if (driver.findElementByTagName("body").getText().length() < MIN_SOURCE_DIFF) {
                                undo = true;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            undo = true;
                        }
                        try {
                            if (undo) {
                                if (origHandle.equals(newHandle)) {
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        try {
                                            driver.navigate().back();
                                        } catch (Throwable t) {
                                            Log.exception(t);
                                        }
                                    }
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        driver.get(origUrl);
                                    }
                                } else {
                                    Util.cleanUpNewWindows(driver, origHandle);
                                }
                            } else {
                                Util.cleanUpNewWindows(driver, newHandle);
                                break;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                    }
                }
            } catch (Throwable t) {
                Log.exception(t);
                continue;
            }
        }
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}