Example usage for org.openqa.selenium WebElement findElements

List of usage examples for org.openqa.selenium WebElement findElements

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement findElements.

Prototype

@Override
List<WebElement> findElements(By by);

Source Link

Document

Find all elements within the current context using the given mechanism.

Usage

From source file:com.waku.mmdataextract.SalesIssues2.java

License:Open Source License

/**
 * @param args/*from ww w. j  av a  2s.c  om*/
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    String formUrl = "http://shouji.gd.chinamobile.com/gdmobile/productDetail/operatorssalesHeaderAction.do";
    String fromUrl2 = "http://shouji.gd.chinamobile.com/gdmobile/productDetail/operatorssalesHeaderAction.do?provinceID=1&cityID=";
    String baseUrl = "http://shouji.gd.chinamobile.com/gdmobile/productDetail/operatorssalesSearchAction.do?type=1&brandId=0&productId=0&provinceId=1";

    WebDriver driver = new ChromeDriver();

    // Get the citySet from form url
    System.out.println("Go to url => " + formUrl);
    driver.get(formUrl);
    WebElement citySelector = driver.findElement(By.id("cityID"));
    List<WebElement> cityOptions = citySelector.findElements(By.tagName("option"));
    Map<String, String> cityIdNameMap = new HashMap<String, String>();
    for (WebElement cityOption : cityOptions) {
        cityIdNameMap.put(cityOption.getAttribute("value"), cityOption.getText());
    }
    System.out.println("Find cities: \n" + cityIdNameMap);
    // A class to hold Sales issue id and name
    class SalesIssue {

        @Override
        public String toString() {
            return id + ":" + name;
        }

        public SalesIssue(String id, String name) {
            this.id = id;
            this.name = name;
        }

        String id;
        String name;
    }

    // Get sales issues info
    Map<String, Set<SalesIssue>> citySalesIssueMap = new HashMap<String, Set<SalesIssue>>();
    for (String cityId : cityIdNameMap.keySet()) {
        String cityUrl = fromUrl2 + cityId;
        driver.get(cityUrl);
        System.out.println("Go to url => " + cityUrl);
        WebElement salesIssueSelector = driver.findElement(By.id("salesissue"));
        List<WebElement> salesIssueOptions = salesIssueSelector.findElements(By.tagName("option"));
        if (salesIssueOptions.get(0).getAttribute("value").equals("0")) {
            System.out.println("No sales issues for " + cityIdNameMap.get(cityId));
        } else {
            Set<SalesIssue> salesIssueSet = new HashSet<SalesIssue>();
            for (WebElement salesIssueOption : salesIssueOptions) {
                SalesIssue salesIssue = new SalesIssue(salesIssueOption.getAttribute("value"),
                        salesIssueOption.getText());
                salesIssueSet.add(salesIssue);
            }
            citySalesIssueMap.put(cityId, salesIssueSet);
            System.out.println("Find sales issues: \n" + citySalesIssueMap);
        }
    }

    for (String cityId : cityIdNameMap.keySet()) {
        for (SalesIssue salesIssue : citySalesIssueMap.get(cityId)) {
            String finalUrl = baseUrl + "&cityId=" + cityId + "&salesissue=" + salesIssue.id;
            driver.get(finalUrl);
            System.out.println("Go to url => " + finalUrl);
            System.out
                    .println("Province,,City," + cityIdNameMap.get(cityId) + ",Sales," + salesIssue.name);
            System.out.println(driver.getPageSource());
        }
    }
}

From source file:com.waku.mmdataextract.SalesIssues3.java

License:Open Source License

public static void main(String[] args) throws Exception {
    FileWriter fw = new FileWriter("Output.csv");
    Map<String, String> cityIdNameMap = new HashMap<String, String>();
    Map<String, Set<SalesIssue>> citySalesIssueMap = new HashMap<String, Set<SalesIssue>>();

    String cityMapString = "116=??, 125=, 117=, 127=, 114=, 115=?, 112=?, 122=, 113=, 110=, 123=, 124=?, 230=, 272=?, 129=, 118=, 119=, 109=, 107=, 106=, 105=, 104=";
    String[] cityMaps = cityMapString.split(", ");
    for (String cityMap : cityMaps) {
        String[] cityIdName = cityMap.split("=");
        cityIdNameMap.put(cityIdName[0], cityIdName[1]);
    }/*from www  . j  a  v  a2  s .  c  o m*/

    String salesIssues = "113=[310,471:?--20110101, 311,310:--20101116, 156,133:?--?20108, 154,132:?3G--20108, 312,469:?--20110101, 149,171:??--20100907, 217,235:??--HZ20101012, -1:?, 311,470:--20110101, 312,311:?--20101116, 156,231:?--HZ20101012, 216,234:?--HZ20101012, 310,309:?--20101116, 215,472:?--20110101, 215,233:?--HZ20101012, 149,129:??--20108, 156,170:?--20100907, 149,232:??--HZ20101012, 154,169:?3G--20100907, 154,212:?3G--HZ20101012], 107=[250,449:?--20110101, 250,369:?--20101215, 250,251:?--20101018, -1:?, 190,190:?--20100825, 249,249:?--20101018, 229,350:--20101215, 189,189:?8+1--20100825, 249,349:?--20101215, 229,230:--20101015, 249,450:?--20110101, 229,451:--20110101], 106=[209,209:?--20101010, 210,210:?--20101010, 210,510:?--20110101, 211,511:--20110101, 209,390:?--20101216, 211,211:--20101010, 209,509:?--20110101, 210,389:?--20101216, 211,391:--20101216, -1:?], 105=[46,46:5--0507, 45,45:?--?, 44,44:?9?--20100507, -1:?], 104=[-1:?, 66,64:?--20100624, 23,107:?0--20100721, 66,108:?--20100721, 23,23:?0--201000624, 68,65:0--2G20100624, 271,270:--20101020, 290,430:--20110101, 290,289:--20101109, 269,269:?--20101020, 68,149:0--20100830, 66,151:?--20100830, 273,271:--20101020, 23,150:?0--20100830, 269,429:?--20110101], 119=[104,329:?--20101209, 291,409:--20101223, 291,489:--20110101, 124,229:?--20101014, -1:?, 124,490:?--20110101, 124,104:?--20100714, 291,290:--20101109, 104,84:?--20100708";
    String[] forCity = salesIssues.split("], ");
    for (String eachCity : forCity) {
        String cityId = eachCity.substring(0, eachCity.indexOf("="));
        String salesIssuesForCity = eachCity.substring(eachCity.indexOf("=") + 2);
        Set<SalesIssue> salesIssueSet = new HashSet<SalesIssue>();
        for (String eachSales : salesIssuesForCity.split(", ")) {
            String[] a = eachSales.split(":");
            if (!a[0].equals("-1")) {
                SalesIssue salesIssue = new SalesIssue(a[0], a[1]);
                salesIssueSet.add(salesIssue);
            }
        }
        citySalesIssueMap.put(cityId, salesIssueSet);
    }

    String baseUrl = "http://shouji.gd.chinamobile.com/gdmobile/productDetail/operatorssalesSearchAction.do?type=1&brandId=0&productId=0&provinceId=1";
    WebDriver driver = new ChromeDriver();
    for (String cityId : citySalesIssueMap.keySet()) {
        for (SalesIssue salesIssue : citySalesIssueMap.get(cityId)) {
            String finalUrl = baseUrl + "&cityId=" + cityId + "&salesissue=" + salesIssue.id;
            driver.get(finalUrl);
            System.out.println("Go to url => " + finalUrl);
            fw.write("Go to url => " + finalUrl + "\n");
            System.out
                    .println("Province,,City," + cityIdNameMap.get(cityId) + ",Sales," + salesIssue.name);
            fw.write("Province,,City," + cityIdNameMap.get(cityId) + ",Sales," + salesIssue.name + "\n");
            List<WebElement> tables = driver.findElements(By.tagName("table"));
            // Second table
            WebElement mainTable = tables.get(1);
            List<WebElement> trs = mainTable.findElements(By.tagName("tr"));
            Boolean firstOne = true;
            int cols = 0;
            for (WebElement tr : trs) {
                try {
                    if (firstOne) {
                        StringBuilder header = new StringBuilder();
                        firstOne = false;
                        List<WebElement> tds = tr.findElements(By.tagName("td"));
                        for (WebElement td : tds) {
                            try {
                                String s = td.findElement(By.tagName("strong")).getText();
                                header.append(s + ",");
                            } catch (NoSuchElementException e) {
                                header.append("ImageURL,");
                            }
                        }
                        System.out.println(header.toString());
                        fw.write(header.toString() + "\n");
                        cols = header.toString().split(",").length;
                    } else {
                        List<WebElement> tds = tr.findElements(By.tagName("td"));
                        if (tds.size() == cols) {
                            StringBuilder line = new StringBuilder();
                            for (WebElement td : tds) {
                                try {
                                    String s = "http://shouji.gd.chinamobile.com"
                                            + td.findElement(By.tagName("img")).getAttribute("src");
                                    line.append(s + ",");
                                } catch (NoSuchElementException e) {
                                    line.append(td.getText() + ",");
                                }
                            }
                            System.out.println(line);
                            fw.write(line.toString() + "\n");
                        } else {
                            StringBuilder line = new StringBuilder();
                            for (int i = 0; i < (cols - tds.size()); i++) {
                                line.append(",");
                            }
                            for (WebElement td : tds) {
                                line.append(td.getText() + ",");
                            }
                            System.out.println(line);
                            fw.write(line.toString() + "\n");
                        }
                    }
                } catch (NoSuchElementException e) {
                    System.out.println("One page end!");
                    fw.write("One page end!" + "\n");
                }
            }

            // Third table
            WebElement lastTable = tables.get(1);
            List<WebElement> span = lastTable.findElements(By.xpath("//span[@class='font_b2']"));
            int pageNumber = Integer.parseInt(span.get(1).getText());
            if (pageNumber > 1) {
                for (int i = 2; i <= pageNumber; i++) {
                    goToURL(fw, driver, finalUrl + "&currentPage=" + i);
                }
            }
        }
    }
    fw.close();
}

From source file:com.waku.mmdataextract.SalesIssues3.java

License:Open Source License

private static void goToURL(FileWriter fw, WebDriver driver, String finalUrl) throws Exception {
    driver.get(finalUrl);/*from   ww w  .  j av  a  2 s .co  m*/
    List<WebElement> tables = driver.findElements(By.tagName("table"));
    // Second table
    WebElement mainTable = tables.get(1);
    List<WebElement> trs = mainTable.findElements(By.tagName("tr"));
    Boolean firstOne = true;
    int cols = 0;
    for (WebElement tr : trs) {
        try {
            if (firstOne) {
                StringBuilder header = new StringBuilder();
                firstOne = false;
                List<WebElement> tds = tr.findElements(By.tagName("td"));
                for (WebElement td : tds) {
                    try {
                        String s = td.findElement(By.tagName("strong")).getText();
                        header.append(s + ",");
                    } catch (NoSuchElementException e) {
                        header.append("ImageURL,");
                    }
                }
                System.out.println(header.toString());
                fw.write(header + "\n");
                cols = header.toString().split(",").length;
            } else {
                List<WebElement> tds = tr.findElements(By.tagName("td"));
                if (tds.size() == cols) {
                    StringBuilder line = new StringBuilder();
                    for (WebElement td : tds) {
                        try {
                            String s = "http://shouji.gd.chinamobile.com"
                                    + td.findElement(By.tagName("img")).getAttribute("src");
                            line.append(s + ",");
                        } catch (NoSuchElementException e) {
                            line.append(td.getText() + ",");
                        }
                    }
                    System.out.println(line);
                    fw.write(line.toString() + "\n");
                } else {
                    StringBuilder line = new StringBuilder();
                    for (int i = 0; i < (cols - tds.size()); i++) {
                        line.append(",");
                    }
                    for (WebElement td : tds) {
                        line.append(td.getText() + ",");
                    }
                    System.out.println(line);
                    fw.write(line.toString() + "\n");
                }
            }
        } catch (NoSuchElementException e) {
            System.out.println("One page end!");
            fw.write("One page end!".toString() + "\n");
        }
    }
}

From source file:com.watchrabbit.crawler.auth.service.AuthServiceImpl.java

License:Apache License

private WebElement locateLoginForm(RemoteWebDriver driver) {
    for (WebElement form : driver.findElements(By.xpath("//form"))) {
        LOGGER.debug("Looking to form with action {}", form.getAttribute("action"));
        List<WebElement> inputs = form.findElements(By.xpath(".//input")).stream()
                .filter(input -> isLoginInput(input) || isPasswordInput(input))
                .filter(input -> input.isDisplayed()).collect(toList());
        if (inputs.size() == 2) {
            return form;
        }/*from w w w.  ja v a 2  s . co  m*/
    }
    return null;
}

From source file:com.watchrabbit.crawler.executor.service.CrawlExecutorServiceImpl.java

License:Apache License

private Optional<SearchForm> findSearchInput(RemoteWebDriver driver) {
    for (WebElement form : driver.findElements(By.xpath("//form"))) {
        LOGGER.debug("Looking to form with action {}", form.getAttribute("action"));
        List<WebElement> inputs = form.findElements(By.xpath(".//input")).stream()
                .filter(input -> input.getAttribute("type").equals("text")
                        || input.getAttribute("type").equals("search"))
                .filter(input -> input.isDisplayed()).collect(toList());
        List<WebElement> passwords = form.findElements(By.xpath(".//input")).stream()
                .filter(input -> input.getAttribute("type").equals("password"))
                .filter(input -> input.isDisplayed()).collect(toList());
        if (inputs.size() == 1 && passwords.isEmpty()) {
            List<WebElement> submit = form.findElements(By.xpath(".//button[@type='submit']"));
            if (submit.isEmpty()) {
                submit = form.findElements(By.xpath(".//input[@type='submit']"));
            }/*from   w w w.j  a v a 2  s.co  m*/
            if (!submit.isEmpty()) {
                return Optional.of(new SearchForm(inputs.get(0), submit.get(0)));
            }
        }
    }
    LOGGER.error("Cannot find form in gateway page");
    return Optional.<SearchForm>empty();
}

From source file:com.watchrabbit.scanner.supervisor.service.FormAnalyzerServiceImpl.java

License:Apache License

private void locateSendButton(WebElement formElement, Form form) throws InvalidFormStructureException {
    List<WebElement> submit = formElement.findElements(By.xpath(".//button[@type='submit']"));
    if (submit.isEmpty()) {
        submit = formElement.findElements(By.xpath(".//input[@type='submit']"));
    }//www. jav  a  2  s  . c o  m
    if (!submit.isEmpty()) {
        form.setSendButton(submit.get(0));
    } else {
        Optional<WebElement> findAny = formElement.findElements(By.xpath(".//input")).stream()
                .filter(input -> input.isDisplayed()).filter(input -> input.isEnabled()).findAny();
        form.setSendButton(
                findAny.orElseThrow(() -> new InvalidFormStructureException("Cannot locate send button")));
    }
}

From source file:com.watchrabbit.scanner.supervisor.service.FormAnalyzerServiceImpl.java

License:Apache License

private List<WebElement> locateElements(WebElement formElement) {
    return formElement.findElements(By.xpath(".//*[self::input or self::textarea or self::select]"));
}

From source file:com.watchrabbit.scanner.supervisor.service.FormAnalyzerServiceImpl.java

License:Apache License

private String extractLabel(WebElement formElement, int i) {
    List<WebElement> labels = formElement
            .findElements(By.xpath("((.//*[self::input or self::textarea or self::select])[" + (i + 1)
                    + "]/preceding::label)[last()]"));
    if (!labels.isEmpty()) {
        WebElement labelElement = labels.get(0);
        return labelElement.getText();
    }//from w w w.  jav a2 s.c  o  m
    return null;
}

From source file:com.watchrabbit.scanner.supervisor.service.SupervisorServiceImpl.java

License:Apache License

private boolean isOpenForAttack(WebElement form) {
    return form.findElements(By.xpath(".//input")).stream().filter(field -> field.isDisplayed())
            .filter(field -> field.isEnabled()).anyMatch(input -> isAnAttackVector(input));
}

From source file:com.wikia.webdriver.pageobjectsfactory.componentobject.visualeditordialogs.VisualEditorAddMediaDialog.java

License:Creative Commons License

public VisualEditorPageObject addExistingMedia(int number) {
    waitForDialogVisible();/*w w w.ja va 2  s.co  m*/
    WebElement mediaResultsWidget = mediaDialogBody.findElement(mediaResultsWidgetBy);
    waitForElementVisibleByElement(mediaResultsWidget);
    List<WebElement> mediaResults = mediaResultsWidget.findElements(mediaResultsBy);
    for (int i = 0; i < number; i++) {
        WebElement mediaAddIcon = mediaResults.get(i).findElement(mediaAddIconBy);
        mediaAddIcon.click();
    }
    clickAddMediaButton();
    waitForDialogNotVisible();
    return new VisualEditorPageObject(driver);
}