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:calc.CalculatorUIT.java

public void testCodesCrud(DesiredCapabilities browser) throws Exception {

    ExtentReports logger = new ExtentReports("target//advancedReport.html", false);
    ExtentTest test1 = logger.startTest("Verify Target String");
    ExtentTest test2 = logger.startTest("Verify Calculation Result");

    WebDriver driver = null;
    System.out.println("Attempt connect to Selenium Node @ " + NodeURL);
    File pathToBinary = new File("/opt/firefox46/firefox/firefox-bin");
    FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    driver = new FirefoxDriver(ffBinary, firefoxProfile);
    try {//w ww  .  j av a2s  .  c om
        System.out.println("attempt connect to target: " + TargetURL);
        driver.get(TargetURL);
        //input a addition 12+8 example in the calculator web-application;
        driver.findElement(By.xpath("(//input[@id='buttonRow'])[2]")).click();
        driver.findElement(By.xpath("(//input[@id='buttonRow'])[3]")).click();
        driver.findElement(By.xpath("(//input[@id='buttonRow'])[12]")).click();
        driver.findElement(By.xpath("(//input[@id='buttonRow'])[10]")).click();
        driver.findElement(By.xpath("(//input[@id='buttonRow'])[15]")).click();

        //search target string value
        String targetStr = "Welcome to the Demo";

        //verify result
        boolean isFound = (driver.findElement(By.tagName("body")).getText()).contains(targetStr);
        boolean result = driver.findElement(By.name("display")).getAttribute("value").contains("84");

        //verify title  
        if (isFound == true) {
            test1.log(LogStatus.PASS, "Title has been verified");
        }
        if (isFound == false) {
            test1.log(LogStatus.FAIL, "Title has NOT been verified");
        }

        //verify result     
        if (result == true) {
            test2.log(LogStatus.PASS,
                    "The Calculation result was correct: [12 multiply by 7 gave output of 84]");
        } else {
            test2.log(LogStatus.FAIL,
                    "The Calculation result was not correct [the title is[Welcome to the Demo!]");

        }
        logger.endTest(test1);
        logger.endTest(test2);
        logger.flush();

        System.out.println("look for= [" + targetStr + "]");
        System.out.println("did we find it? [" + isFound + "]");

        System.out.println("calculate 12 multiply by 7");
        System.out.println("Was the result 84 ? [" + result + "]");

        Assert.assertTrue(isFound);
        Assert.assertTrue(result);
        //  Assert.assertEquals("hell",driver.getTitle());
        //doTest(driver); 
        // rest of test commands come here
    } catch (Exception e) {
        e.printStackTrace();
        Assert.assertTrue(e.getMessage(), false);
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}

From source file:capaPresentacio.FirefoxDriverTest.java

/**
 * @param args//  www  .  jav  a  2s  .  c  o  m
 * @throws java.io.IOException
 */
public static void main(String[] args) throws IOException {
    // Create webDriver reference
    WebDriver driver;

    // Launch FirefoxDriver
    driver = new FirefoxDriver();
    String route = new java.io.File(".").getCanonicalPath();
    System.out.println("file:/" + route + "/index.html");
    driver.get("file://" + route + "/index.html");
}

From source file:ch.vorburger.vaadin.designer.tests.web.VaadinServerTest.java

License:Apache License

@Test
@Ignore // TODO reactivate when http://dev.vaadin.com/ticket/9386 is fixed?
public void testStartVaadinServer() throws Exception {
    VaadinServer server = new VaadinServer() {
        @Override//from  www  .java2  s .c  o m
        protected Class<? extends Application> getVaadinApplicationClass() {
            return VaadinTestApplication.class;
        }
    };

    try {
        server.start();
        // Check if "Saluton!" button shows up:
        WebDriver driver = new InternetExplorerDriver();
        driver.get(server.getURL());
        WebElement button = driver.findElement(By.id(HTML_ID));
        String text = button.getText();
        Assert.assertEquals("Saluton!", text);
        driver.quit();

    } finally {
        server.stop();
    }
}

From source file:cn.edu.hfut.dmic.webcollector.example.FirefoxSelenium3.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override/*from w w w . j a va  2  s  .co m*/
        public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            // ?
            // DBCollection dbCollection = mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); 
            DB db = mongoClient.getDB("maoyan_crawler");
            // ?????
            Set<String> colls = db.getCollectionNames();
            for (String s : colls) {
                // Collection(?"")
                if (s.equals("attend_rate")) {
                    db.getCollection(s).drop();
                }
            }
            DBCollection dbCollection = db.getCollection("attend_rate");
            ProfilesIni pi = new ProfilesIni();
            FirefoxProfile profile = pi.getProfile("default");
            WebDriver driver = new FirefoxDriver(profile);
            driver.manage().window().maximize();
            driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
            //                driver.setJavascriptEnabled(false);
            driver.get(datum.getUrl());
            //                System.out.println(driver.getPageSource());
            driver.findElement(By.xpath("//*[@id='seat_city']")).click();
            driver.switchTo().window(driver.getWindowHandle());

            int city_num = driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).size();
            for (int i = 0; i < city_num; i++) {
                System.out.println("A city chosen" + i);
                System.out.println(
                        driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i).getText());
                String city = driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i)
                        .getText();
                ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);",
                        driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i));
                ((JavascriptExecutor) driver).executeScript("window.scrollBy(0, -250)", "");
                Thread.sleep(1000);
                new Actions(driver)
                        .moveToElement(
                                driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i))
                        .click().perform();
                driver.switchTo().window(driver.getWindowHandle());
                //                System.out.println(driver.findElement(By.xpath("//span[@class='today']/em")).getText());
                System.out.println(driver.findElement(By.xpath("//span[@class='today']")).getText());
                for (int j = 0; j < driver
                        .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c1 lineDot']"))
                        .size(); j++) {
                    System.out.println(driver
                            .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c1 lineDot']"))
                            .get(j).getText());
                    System.out.println(
                            driver.findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c2 red']"))
                                    .get(j).getText());
                    System.out.println(
                            driver.findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c3 gray']"))
                                    .get(j).getText());
                    BasicDBObject dbObject = new BasicDBObject();
                    dbObject.append("title", driver.findElement(By.xpath("//span[@class='today']")).getText())
                            .append("city", city)
                            .append("mov_cnname",
                                    driver.findElements(
                                            By.xpath("//div[@id='seat_table']//ul//li[@class='c1 lineDot']"))
                                            .get(j).getText())
                            .append("boxoffice_rate", driver
                                    .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c2 red']"))
                                    .get(j).getText())
                            .append("visit_pershow", driver
                                    .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c3 gray']"))
                                    .get(j).getText());
                    dbCollection.insert(dbObject);
                }
                System.out.println("new city list to choose");
                new Actions(driver).moveToElement(driver.findElement(By.xpath("//*[@id='seat_city']"))).click()
                        .perform();
                driver.switchTo().window(driver.getWindowHandle());
                Thread.sleep(500);
            }
            driver.close();
            driver.quit();
            mongoClient.close();
        }
    };

    //DBDBManager
    DBManager manager = new BerkeleyDBManager("crawl");
    //Crawler?DBManagerExecutor
    Crawler crawler = new Crawler(manager, executor);
    crawler.addSeed("http://pf.maoyan.com/attend/rate");
    crawler.start(1);
}

From source file:cn.edu.hfut.dmic.webcollector.example.FirefoxSelenium4.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override/*from   www .j  a  v a2s  .  c  o m*/
        public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            // ?
            // DBCollection dbCollection = mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); 
            DB db = mongoClient.getDB("maoyan_crawler");
            // ?????
            Set<String> colls = db.getCollectionNames();
            for (String s : colls) {
                // Collection(?"")
                if (s.equals("rankings_am")) {
                    db.getCollection(s).drop();
                }
            }
            DBCollection dbCollection = db.getCollection("attend_rate");
            ProfilesIni pi = new ProfilesIni();
            FirefoxProfile profile = pi.getProfile("default");
            WebDriver driver = new FirefoxDriver(profile);
            driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
            //                driver.setJavascriptEnabled(false);
            driver.get(datum.getUrl());
            //                System.out.println(driver.getPageSource());
            List<WebElement> movie_name = driver
                    .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c1 lineDot']"));
            List<WebElement> boxoffice_rate = driver
                    .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c2 red']"));
            List<WebElement> visit_pershow = driver
                    .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c3 gray']"));
            WebElement title = driver.findElement(By.xpath("//span[@class='today']/em"));
            WebElement title2 = driver.findElement(By.xpath("//span[@class='today']"));
            System.out.println(title.getText());
            System.out.println(title.getText());
            for (int i = 0; i < movie_name.size(); i++) {
                System.out.println(movie_name.get(i).getText());
                System.out.println(boxoffice_rate.get(i).getText());
                System.out.println(visit_pershow.get(i).getText());
                //                   BasicDBObject dbObject = new BasicDBObject();
                //                  dbObject.append("title", title).append("rank", amList.get(0)).append("mov_cnname", cn_name).append("mov_enname", en_name).append("toweek_rev", amList.get(2)).append("total_rev", amList.get(3)).append("val_week", amList.get(4));
                //                   dbCollection.insert(dbObject);
            }
            driver.quit();

        }
    };

    //DBDBManager
    DBManager manager = new BerkeleyDBManager("crawl");
    //Crawler?DBManagerExecutor
    Crawler crawler = new Crawler(manager, executor);
    crawler.addSeed("http://pf.maoyan.com/attend/rate");
    crawler.start(1);
}

From source file:co.edu.icesi.i2t.slrtools.bib.transformations.TransformBibACM.java

License:Open Source License

/**
 * Funcion principal para la transformacion de los archivos html en un solo
 * arhivo consolidado BIB, la funcion se encarga de extraer del html cada
 * link de articulo, despues de tener el listado de url se dispone con la
 * ayuda de selenium webdriver el ingreso a cada url y descargar el BIBtex
 * para finalizar con la union de todos en un solo archivo BIB
 *
 * @param sourceFilesPath String con la ruta de la carpeta donde se
 * encuentran los archivos html a transformas
 * @param targetFilePath String con la ruta fisica del directorio donde se
 * guardara el archivo consolodidao//from  ww w  .ja  v a2s . com
 * @param targetFileName
 */
public static boolean transformFiles(String sourceFilesPath, String targetFilePath, String targetFileName) {
    boolean bibFileCreated = false;

    System.out.println("");
    System.out.println("-----------------------------");
    System.out.println("Generating BibTeX for ACM Digital Library results...");
    System.out.println("");

    String bibContent = "";
    List<String> urls = extractURL(sourceFilesPath);
    FirefoxProfile profile = new FirefoxProfile();
    WebDriver webDriver = new FirefoxDriver(profile);
    for (int i = 0; i < urls.size(); i++) {
        try {
            System.out.println("[INFO] Retrieving BibTeX from URL: " + urls.get(i));
            String idFile = urls.get(i).split("id=")[1].split("&")[0];
            idFile = idFile.substring(idFile.indexOf(".") + 1, idFile.length());
            String url = (urls.get(i));
            webDriver.get(url);
            WebElement bibField = (new WebDriverWait(webDriver, 10))
                    .until(ExpectedConditions.presenceOfElementLocated(By.linkText("BibTeX")));
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {

            }
            bibField.click();
            WebElement textBib = (new WebDriverWait(webDriver, 10))
                    .until(ExpectedConditions.presenceOfElementLocated(By.id(idFile)));
            String reference = textBib.getText();
            String referenceAbstract = webDriver.findElement(By.id("abstract")).getText();
            referenceAbstract = "abstract = {" + referenceAbstract + "}}" + System.lineSeparator();
            bibContent += reference.substring(0, reference.length() - 2) + referenceAbstract;
            //Thread.sleep(10000);
            //tempurl.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            System.out.println(
                    "[ERROR] Failed to retrieve BibTex. The application may have been blocked by ACM Digital Library. Try again later.");
        } catch (Exception e) {
            System.out.println("[ERROR] Failed to retrieve BibTeX. " + e.getMessage());
        }
    }
    webDriver.quit();
    try {
        saveBibFile(bibContent, targetFilePath, targetFileName);
        if (!bibContent.equals("")) {
            bibFileCreated = true;
        }
    } catch (Exception e) {
        System.out.println("[ERROR] Failed to create BibTeX file." + e.getMessage());
    }
    System.out.println("-----------------------------");
    return bibFileCreated;
}

From source file:co.edu.icesi.i2t.slrtools.webdrivers.WebDriverACM.java

License:Open Source License

/**
 * Funcion que se encarga de realizar automaticamente la busqueda en la base
 * de datos ACM Digital Library conforme a una cadena de busqueda
 * introducida, ACM digital library no permite la descarga de ningun archivo
 * del resultado de la busqueda, por tal razn, se descarga el codigo fuente
 * de la pagina en el source del proyecto el cual posteriormente es usado
 * para la extraccin de la informacin y construccion del BIB con los
 * resultados obtenidos//ww  w .  j a v  a  2 s .  c  o  m
 *
 * @param searchStrings este parametro es la cadena de busqueda que retorna
 * la funcion mixACM#mixWords2, cada cadena de busqueda esta separada por ;
 * @param url este paremetro es el URL de la busqueda avanzada de ACM
 * Digital Library
 * @see mixWords.mixACM#mixWords2(java.lang.String, java.lang.String)
 */
public static void searchWeb(String searchStrings, String url) {
    /* a esta funcion se debe mejorar
     * 1: validar el boton siguiente sin try catch, mejorar el manejo de las expeciones
     */
    System.out.println("");
    System.out.println("-----------------------------");
    System.out.println("Searching ACM Digital Library...");
    System.out.println("Search strings: " + searchStrings + "");
    System.out.println("");

    FirefoxProfile profile = new FirefoxProfile();
    WebDriver webDriver = new FirefoxDriver(profile);
    String[] strings = searchStrings.split(";");
    for (int i = 0; i < strings.length; i++) {
        try {
            webDriver.get(url);

            WebElement searchField = webDriver.findElement(By.name("within"));
            searchField.click();
            searchField.sendKeys(strings[i]);

            WebElement buttonSearch = webDriver.findElement(By.name("Go"));
            buttonSearch.click();

            List<WebElement> stringResult = webDriver
                    .findElements(By.xpath("//span[contains(@style, 'background-color:yellow')]"));
            if (!stringResult.isEmpty()) {
                System.out.println(
                        "[WARNING] Search string " + (i + 1) + ": " + strings[i] + " retrieves no results");
            } else {
                int counter = 1;
                try {
                    WebElement nextField = null;
                    do {
                        String sourceCode = webDriver.getPageSource();
                        File targetDirectory = new File("files" + File.separator + Database.ACM.getName());
                        targetDirectory.mkdir();
                        try (PrintWriter file = new PrintWriter(
                                "files" + File.separator + Database.ACM.getName() + File.separator
                                        + "searchResults_" + i + "_" + counter + ".html",
                                "UTF-8")) {
                            file.print(sourceCode);
                        }
                        nextField = (new WebDriverWait(webDriver, 10))
                                .until(ExpectedConditions.presenceOfElementLocated(By.linkText("next")));
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {

                        }
                        nextField.click();
                        counter++;
                    } while (true);
                } catch (NoSuchElementException | TimeoutException | NullPointerException e) {
                    System.out.println("[INFO] Search string " + (i + 1) + ": " + strings[i] + " has "
                            + counter + (counter == 1 ? " result" : " results") + "returned");
                }
            }
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            System.out.println(
                    "[ERROR] Search string " + (i + 1) + ": " + strings[i] + " failed. " + e.getMessage());
        } catch (NoSuchElementException e) {
            System.out.println(
                    "[ERROR] The application has been blocked by ACM Digital Library. Try again later.");
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {

        }
    }
    webDriver.quit();
    System.out.println("[INFO] Finished search in ACM Digital Library");
    System.out.println("-----------------------------");
}

From source file:co.edu.icesi.i2t.slrtools.webdrivers.WebDriverIEEEComputerScience.java

License:Open Source License

/**
 * /* ww  w .j ava  2  s. c  o m*/
 * @param cadenasBusqueda
 * @param direccionBusqueda
 */
public static void searchWeb(String cadenasBusqueda, String direccionBusqueda) {
    /* a esta funcion se debe mejorar
     * 1: automatizar las preferencias para la descarga automatica
     * 2: la ruta de descarga
     * 3: el cierre del navegador al finalizar la ultima descarga*/
    System.out.println("----------inicia busqueda web IEEE Computer Society----------");
    System.out.println("Cadena de palabras a buscar: " + cadenasBusqueda);
    System.out.println("------------------------------------------------------");
    FirefoxProfile profile = new FirefoxProfile();
    WebDriver url = new FirefoxDriver(profile);
    String[] lcadenasBusqueda = cadenasBusqueda.split(";");
    for (int i = 0; i < lcadenasBusqueda.length; i++) {
        try {
            url.get(direccionBusqueda);
            WebElement searchField = url.findElement(By.name("queryText1"));
            WebElement buttonSearch = url.findElement(By.id("searchButton"));
            searchField.click();
            searchField.sendKeys(lcadenasBusqueda[i]);
            buttonSearch.click();
            WebElement selectField = url.findElement(By.id("select1"));
            WebElement buttonField = url.findElement(By.id("form1Button"));
            selectField.sendKeys("100");
            buttonField.click();
            try {
                WebElement stringResult = url
                        .findElement(By.xpath("//div[contains(@class, 'searchwhitebox')]"));
                System.out.println("Busqueda: " + i + " cadena de busqueda:" + lcadenasBusqueda[i] + " - "
                        + stringResult.getText());
                //String sourceCode=url.getPageSource();
                //PrintWriter archivo = new PrintWriter("busquedasIEEECS/"+lcadenasBusqueda[i]+".html", "UTF-8");
                //archivo.print(sourceCode);
                //archivo.close();              
            } catch (Exception e) {
                System.out.println("[OJO]" + " Busqueda: " + i + " la cadena de busqueda: "
                        + lcadenasBusqueda[i] + " no trae resultados");
            }
        } catch (Exception e) {
            System.out
                    .println("excepcion busqueda cadena: " + lcadenasBusqueda[i] + " , motivo excepcion: " + e);
        }
    }
    //url.quit();
    System.out.println("----------fin busqueda web IEEE Computer Society----------");
}

From source file:co.edu.icesi.i2t.slrtools.webdrivers.WebDriverIEEEXplore.java

License:Open Source License

/**
 * Funcion que se encarga de realizar automaticamente la busqueda en la base
 * de datos IEEEXplore conforme a una cadena de busqueda introducida, la
 * automatizacion nos permite descargar los CSV que IEEEXplore dispone con
 * el resultaod de las busquedas, la ruta donde se guardan los archivos
 * dependen de la seleccin del usuario, si por alguna razon el resultado de
 * la busqueda es mayor a 2000 datos, el buscador solo deja descargar los
 * primeros 2000 ordenados por importancia de acuerdo a las politicas de
 * IEEEXplore, los archivos descargados posteriormente son procesadas para
 * la construccion del BIB con los resultados obtenidos.
 *
 * @param searchStrings este parametro es la cadena de busqueda que retorna
 * la funcion mixIEEEXplore#mixWords, cada cadena de busqueda esta separada
 * por ;/*from   ww  w. j  a v a  2 s  .c om*/
 * @param url este paremetro es el URL de la busqueda avanzada de IEEEXplore
 * @see mixWords.mixIEEEXplore#mixWords(java.lang.String, java.lang.String)
 */
public static void searchWeb(String searchStrings, String url) {
    System.out.println("");
    System.out.println("-----------------------------");
    System.out.println("Searching IEEE Xplore Digital Library...");
    System.out.println("Search strings: " + searchStrings + "");
    System.out.println("");

    String workingDir = System.getProperty("user.dir");

    File targetDirectory = new File("files" + File.separator + Database.IEEE_XPLORE.getName());
    targetDirectory.mkdir();

    FirefoxProfile profile = new FirefoxProfile();

    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.dir",
            workingDir + File.separator + "files" + File.separator + Database.IEEE_XPLORE.getName());
    profile.setPreference("browser.download.useDownloadDir", true);
    profile.setPreference("browser.helperapps.neverAsk.saveToDisk", "application/x-latex;text/csv");
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.closeWhenDone", true);

    WebDriver webDriver = new FirefoxDriver(profile);

    String[] strings = searchStrings.split(";");
    for (int i = 0; i < strings.length; i++) {
        try {
            webDriver.get(url);
            WebElement searchField = webDriver.findElement(By.id("expression-textarea"));
            WebElement buttonSearch = webDriver.findElement(By.id("submit-search"));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
            searchField.click();
            searchField.sendKeys(strings[i]);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
            buttonSearch.click();
            try {
                WebElement exportField = (new WebDriverWait(webDriver, 10))
                        .until(ExpectedConditions.presenceOfElementLocated(By.id("popup-export-results")));
                WebElement stringResult = webDriver
                        .findElement(By.xpath("//div[contains(@id, 'content')]/span"));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {

                }
                exportField.click();
                System.out.println("[INFO] Search string " + (i + 1) + " " + strings[i] + " "
                        + stringResult.getText());
                WebElement exportButton = (new WebDriverWait(webDriver, 10))
                        .until(ExpectedConditions.presenceOfElementLocated(By.id("export_results_ok")));
                exportButton.click();
            } catch (Exception e) {
                System.out.println(
                        "[WARNING] Search string " + (i + 1) + " " + strings[i] + " retrieves no results");
            }
        } catch (Exception e) {
            System.out.println(
                    "[ERROR] Search string " + (i + 1) + " " + strings[i] + " failed. " + e.getMessage());
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {

        }
    }
    //        webDriver.quit();
    System.out.println("[INFO] Finished search in IEEE Xplore Digital Library");
    System.out.println("-----------------------------");
}

From source file:co.edu.icesi.i2t.slrtools.webdrivers.WebDriverScienceDirect.java

License:Open Source License

/**
 * Funcion que se encarga de realizar automaticamente la busqueda en la base
 * de datos ScienceDirect conforme a una cadena de busqueda introducida, la
 * automatizacion nos permite descargar los BIB que ScienceDirect dispone
 * con el resultaod de las busquedas, la ruta donde se guardan losr archivos
 * dependen de la seleccin del usuario, si por alguna razon el resultado de
 * la busqueda es mayor a 1000 datos, el buscador solo deja descargar los
 * primeros 1000 ordenados por importancia de acuerdo a las politicas de
 * ScienceDirect, los archivos descargados posteriormente son procesadas
 * para la construccion del BIB consolodidado con los resultados obtenidos.
 *
 * @param searchStrings este parametro es la cadena de busqueda que retorna
 * la funcion mixScienceDirect#mixWords, cada cadena de busqueda esta
 * separada por ;//from   w  w w  .j  av  a 2 s  . c o m
 * @param url este paremetro es el URL de la busqueda experta de
 * ScienceDirect
 * @see mixWords.mixScienceDirect#mixWords(java.lang.String,
 * java.lang.String)
 */
public static void searchWeb(String searchStrings, String url) {
    System.out.println("");
    System.out.println("-----------------------------");
    System.out.println("Searching Science Direct...");
    System.out.println("Search strings: " + searchStrings + "");
    System.out.println("");

    String workingDir = System.getProperty("user.dir");

    File targetDirectory = new File("files" + File.separator + Database.SCIENCE_DIRECT.getName());
    targetDirectory.mkdir();

    FirefoxProfile profile = new FirefoxProfile();

    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.dir",
            workingDir + File.separator + "files" + File.separator + Database.SCIENCE_DIRECT.getName());
    profile.setPreference("browser.download.useDownloadDir", true);
    profile.setPreference("browser.helperapps.neverAsk.saveToDisk", "application/x-latex;text/csv");
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.closeWhenDone", true);
    profile.setPreference("browser.download.panel.shown", true);

    WebDriver webDriver = new FirefoxDriver(profile);

    String[] strings = searchStrings.split(";");
    for (int i = 0; i < strings.length; i++) {
        try {
            webDriver.get(url);
            WebElement searchField = webDriver.findElement(By.name("SearchText"));
            //              WebElement searchField = (new WebDriverWait(webDriver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.name("SearchText")));
            WebElement submitSearch = webDriver.findElement(By.name("RegularSearch"));
            //              WebElement submitSearch = (new WebDriverWait(webDriver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.name("RegularSearch")));
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {

            }
            searchField.click();
            searchField.sendKeys(strings[i]);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {

            }
            submitSearch.click();
            try {
                WebElement exportButton = (new WebDriverWait(webDriver, 10)).until(ExpectedConditions
                        .presenceOfElementLocated(By.cssSelector("span.down_sci_dir.exportArrow")));
                WebElement stringResult = webDriver
                        .findElement(By.xpath("//h1[contains(@class, 'queryText')]/strong"));
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {

                }
                exportButton.click();
                System.out.println("[INFO] Search string " + (i + 1) + " " + strings[i] + " "
                        + stringResult.getText());
                WebElement bibTex = webDriver.findElement(By.id("BIBTEX"));
                bibTex.click();
                WebElement export = webDriver.findElement(By.id("export_button"));
                export.click();
            } catch (Exception e) {
                System.out.println(
                        "[WARNING] Search string " + (i + 1) + " " + strings[i] + " retrieves no results");
            }
        } catch (Exception e) {
            System.out.println(
                    "[ERROR] Search string " + (i + 1) + " " + strings[i] + " failed. " + e.getMessage());
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {

        }
    }
    //   webDriver.quit();
    System.out.println("[INFO] Finished search in Science Direct");
    System.out.println("-----------------------------");
}