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:com.waku.mmdataextract.IMEI.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    WebDriver driver = new HtmlUnitDriver();
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("Brand.xml");
    List<Element> brands = new SAXReader().read(in).selectNodes("/brand/option");
    for (Element brand : brands) {
        String bId = brand.attributeValue("value");
        FileWriter fw = new FileWriter(new File("output/" + brand.getText() + ".csv"));
        try {//from w w w  .ja  v a 2 s. c o  m
            Document doc = HttpCaller.httpGetAsXMLDoc(urlFor(bId));
            List<Element> products = doc.selectNodes("/products/product");
            for (Element e : products) {
                String qString = e.attributeValue("id");
                driver.get(urlFor(1, qString));
                int pageNumber = Integer
                        .parseInt(driver.findElements(By.xpath("//span[@class='font_b2']")).get(1).getText());
                if (pageNumber >= 1) {
                    for (int i = 1; i <= pageNumber; i++) {
                        readForEachPage(fw, driver, urlFor(i, qString));
                    }
                }
            }
            fw.close();
        } catch (RuntimeException e) {
            System.out.println("Skipped " + brand.getText());
            fw.write("Skipped!");
            fw.close();
        }
    }
}

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 w w w .  j  ava 2 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);// w ww  . j  a v a2  s  .c  om
    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.wikia.webdriver.pageobjectsfactory.pageobject.WikiBasePageObject.java

License:Open Source License

public void verifyUserLoggedIn(final String userName) {
    changeImplicitWait(250, TimeUnit.MILLISECONDS);
    try {/*ww  w  .  j  a  v a  2  s .c o  m*/
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                if (body.getAttribute("class").contains("skin-monobook")) {
                    return driver.findElements(By.cssSelector(
                            loggedInUserSelectorMonobook.replace("%userName%", userName.replace(" ", "_"))))
                            .size() > 0;// only for verification
                } else {
                    //Venus
                    return driver
                            .findElements(By
                                    .cssSelector(LOGGED_IN_USER_SELECTOR_VENUS.replace("%userName%", userName)))
                            .size() > 0;// only for verification
                }
            }
        });
    } finally {
        restoreDeaultImplicitWait();
    }
    PageObjectLogging.log("verifyUserLoggedIn", "user " + userName + " logged in", true);
}

From source file:com.worldline.easycukes.selenium.utils.SeleniumHelper.java

License:Open Source License

/**
 * @return/*from  w  w  w.  j  av a  2 s .c o m*/
 */
public static void waitUntilElementIsHidden(@NonNull final WebDriver driver, @NonNull final By by) {
    // Waiting for an element to be present on the page, checking for its
    // presence once every second.
    final FluentWait<WebDriver> wait = new FluentWait<>(driver)
            .withTimeout(WAITING_TIME_OUT_IN_SECONDS, TimeUnit.SECONDS)
            .pollingEvery(POLLING_INTERVAL_IN_MILLIS, TimeUnit.MILLISECONDS).ignoring(TimeoutException.class);

    wait.until(new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver webDriver) {
            try {
                log.debug("waiting for hidden " + by);
                final List<WebElement> list = webDriver.findElements(by);
                for (final WebElement webElement : list)
                    if (webElement.isDisplayed()) {
                        log.debug("Still displayed !");// TODO
                        return false;
                    }
                return true;
            } catch (final StaleElementReferenceException e) {
                return true;
            } catch (final NoSuchElementException e) {
                return true;
            }
        }
    });
}

From source file:com.YOMP.tests.ManageTest.java

License:Open Source License

public static void testDropDown(WebDriver driver) {
    String[] hrefArray = { "manual", "auto", "import", "autostack", "custom" };
    List<WebElement> anchors = driver.findElements(By.cssSelector("div div ul li a"));
    Assert.assertTrue(hrefArray.length == anchors.size());
    for (int i = 0; i < anchors.size(); i++) {
        String hrefString = anchors.get(i).getAttribute("href").toString();
        Assert.assertTrue(hrefString.contains(hrefArray[i]), String.format("%s not present", hrefString));
    }/*from www. j  ava2 s . c o  m*/
}

From source file:com.YOMP.tests.WelcomeTest.java

License:Open Source License

public static void welcomeDropdown(WebDriver driver) {
    String welcomedropDown[] = new String[] { "ap-northeast-1: Asia Pacific (Tokyo)",
            "ap-southeast-1: Asia Pacific (Singapore)", "ap-southeast-2: Asia Pacific (Sydney)",
            "eu-west-1: EU (Ireland)", "sa-east-1: South America (Sao Paulo)",
            "us-east-1: US East (Northern Virginia)", "us-west-1: US West (Northern California)",
            "us-west-2: US West (Oregon)" };
    int ddlength = welcomedropDown.length;
    List<WebElement> regionName = driver.findElements(By.xpath("//select//option"));
    Assert.assertTrue(ddlength == regionName.size());
    for (int locaterIndex = 0; locaterIndex < ddlength; locaterIndex++) {
        String xpathString = String.format("//select//option[%d]", locaterIndex + 1);
        String val = driver.findElement(By.xpath(xpathString)).getText();
        Assert.assertTrue(welcomedropDown[locaterIndex].equalsIgnoreCase(val));
    }/*from   ww  w. j  a va2s  .  c  o  m*/
}

From source file:com.zhao.crawler.demo.DemoJSCrawler.java

License:Open Source License

protected void print(WebDriver driver) {
    List<WebElement> divInfos = driver.findElements(By.cssSelector("li.gl-item"));
    for (WebElement divInfo : divInfos) {
        WebElement price = divInfo.findElement(By.className("J_price"));
        System.out.println(price + ":" + price.getText());
    }/*from   ww w.  ja  va  2 s .c o  m*/
}

From source file:com.zhao.crawler.jd.JDGoodsList.java

License:Open Source License

@Override
public void addGoods(Page page) {
    WebDriver driver = null;
    try {// w  ww .  ja v  a  2  s  .  co  m
        driver = PageUtils.getWebDriver(page);
        List<WebElement> eles = driver.findElements(By.cssSelector("li.gl-item"));
        if (!eles.isEmpty()) {
            for (WebElement ele : eles) {
                Goods g = new Goods();
                g.setPlatform(Platform.JD);// ?
                // 
                String priceStr = ele.findElement(By.className("p-price")).findElement(By.className("J_price"))
                        .findElement(By.tagName("i")).getText();
                if (Tools.notEmpty(priceStr)) {
                    g.setPrice(Float.parseFloat(priceStr));
                } else {
                    g.setPrice(-1f);
                }
                // ???
                g.setName(ele.findElement(By.className("p-name")).findElement(By.tagName("em")).getText());
                // ?
                g.setUrl(ele.findElement(By.className("p-name")).findElement(By.tagName("a"))
                        .getAttribute("href"));
                // 
                String commitStr = ele.findElement(By.className("p-commit")).findElement(By.tagName("a"))
                        .getText();
                if (Tools.notEmpty(commitStr)) {
                    commitStr = "100";
                    g.setCommit(Integer.parseInt(commitStr));
                } else {
                    g.setCommit(-1);
                }

                add(g);
            }
        } else {
            System.out.println("else is empty");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}

From source file:com.zhao.crawler.jd.LoginContentList.java

License:Open Source License

@Override
public void addGoods(Page page) {
    WebDriver driver = null;
    try {//from w  w  w .  ja va  2s . c o m
        System.out.println(page.getResponse().getContent());

        String selector = "root";

        //li.premium-pricecube
        driver = PageUtils.getWebDriver(page);
        List<WebElement> eles = driver.findElements(By.cssSelector(selector));

        if (!eles.isEmpty()) {
            for (WebElement ele : eles) {

                System.out.println(" ### " + ele.getText());
                //               // ?
                //               g.setUrl(ele.findElement(By.className("p-name"))
                //                     .findElement(By.tagName("a"))
                //                     .getAttribute("href"));
                //               // 
                //               String commitStr = ele
                //                     .findElement(By.className("p-commit"))
                //                     .findElement(By.tagName("a"))
                //                     .getText();
                //               if (Tools.notEmpty(commitStr)) {
                //                  commitStr ="100";
                //                  g.setCommit(Integer.parseInt(commitStr));
                //               } else {
                //                  g.setCommit(-1);
                //               }

            }
        } else {
            System.out.println("else is empty");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}