Example usage for org.openqa.selenium WebDriver get

List of usage examples for org.openqa.selenium WebDriver get

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver get.

Prototype

void get(String url);

Source Link

Document

Load a new web page in the current browser window.

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 ww  .j a v  a 2 s .  co  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.IMEI.java

License:Open Source License

private static void readForEachPage(FileWriter fw, WebDriver driver, String url) throws Exception {
    try {//www  . j  av  a 2 s  . c om
        driver.get(url);
        WebElement table = driver.findElement(By.xpath("//table[@bgcolor='BFBEC3']"));
        List<WebElement> items = table.findElements(By.xpath("./tbody/tr"));
        for (int i = 1; i < items.size() - 1; i++) {
            List<WebElement> cols = items.get(i).findElements(By.tagName("td"));
            for (WebElement col : cols) {
                System.out.print(col.getText() + ",");
                fw.write(col.getText() + ",");
            }
            System.out.println();
            fw.write("\n");
        }
    } catch (Exception e) {
        System.out.println("---------Retry--------------------------");
        System.out.println(e.getMessage());
        System.out.println("---------Retry---------------------------");
        readForEachPage(fw, driver, url);
    }
}

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

License:Open Source License

/**
 * @param args/* w w w  .  j  av  a  2s . co  m*/
 * @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   w ww .  j a v a2s.  com

    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);
    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;//from w ww.j ava2  s. c om
    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.webdriver1.MouseOver.java

public static void main(String[] args) throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    //maximize window
    driver.manage().window().maximize();
    //wait for page load
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    //Go to URL/*from   ww  w  .j a v  a 2  s  . c  o m*/
    driver.get("http://flex.apache.org/");
    //Get web element
    WebElement element = driver.findElement(By.xpath("//ul[@id='nav']/li[2]/a"));
    //Thread.sleep(1000);
    //Get sub web element/html/body/div[1]/div[1]/
    WebElement element1 = driver.findElement(By.xpath("//a[contains(text(),'License & Trademarks')]"));
    //Declare Actions Object
    Actions action = new Actions(driver);
    //Mouse over on element items
    action.moveToElement(element).build().perform();
    //wait for items
    Thread.sleep(2000);
    //click on element1 items
    action.moveToElement(element1).clickAndHold().build().perform();

}

From source file:com.wikia.webdriver.pageobjectsfactory.pageobject.WikiBasePageObject.java

License:Open Source License

public void logOut(WebDriver driver) {
    try {//w w  w  . j  a  v a 2s. c  o m
        driver.manage().deleteAllCookies();
        driver.get(Global.DOMAIN + URLsContent.LOGOUT);
    } catch (TimeoutException e) {
        PageObjectLogging.log("logOut", "page loads for more than 30 seconds", true);
    }
    waitForElementPresenceByBy(LOGIN_BUTTON_CSS);
    PageObjectLogging.log("logOut", "user is logged out", true, driver);
}

From source file:com.xpanxion.tests.FasoosTest.java

public void verifySearchItem() {
    WebDriver driver = DriverFactory.getDriverInstance();
    driver.get("https://order.faasos.io/");
}

From source file:com.xpanxion.tests.FasoosTest.java

@Test(dataProvider = DataProviderLibrary.DP_SEARCHFOODITEM, dataProviderClass = DataProviderLibrary.class)
public void verifySearchItem(String searchItem) {
    WebDriver driver = DriverFactory.getDriverInstance();
    driver.get("https://order.faasos.io/");

}

From source file:com.xpanxion.tests.SampleToolsQATest.java

@Test
public void verifyToolsQA_NoData() {
    WebDriver driver = DriverFactory.getDriverInstance();
    driver.get("https://mvnrepository.com/");
}