Example usage for org.jsoup.nodes Document getElementById

List of usage examples for org.jsoup.nodes Document getElementById

Introduction

In this page you can find the example usage for org.jsoup.nodes Document getElementById.

Prototype

public Element getElementById(String id) 

Source Link

Document

Find an element by ID, including or under this element.

Usage

From source file:com.obnsoft.ptcm3.MyApplication.java

private void parseCommandHtml() {
    mCommands = new ArrayList<Command>();
    mCategories = new ArrayList<String>();
    int categoryId = -1;
    try {//from  w  w  w  .j a  v a 2  s.c o m
        InputStream in = openFileInput(FNAME_CMD_HTML);
        Document document = Jsoup.parse(in, "UTF-8", URL_CMD_HTML);
        in.close();
        Element divContentArea = document.getElementById(ID_CONTENTAREA);
        for (Element e : divContentArea.children()) {
            if (e.tagName().equals(TAG_TABLE)) {
                if (e.className().equals("")) {
                    mCommands.add(new Command(e, categoryId));
                }
            } else if (e.tagName().equals(TAG_H3)) {
                mCategories.add(e.text());
                categoryId++;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        mCommands = null;
        mCategories = null;
    }
}

From source file:eu.masconsult.bgbanking.banks.fibank.my.MyFIBankClient.java

private void performLogin(DefaultHttpClient httpClient, AuthToken authToken)
        throws ClientProtocolException, IOException, AuthenticationException {
    obtainAspSessionCookie(httpClient);// w  ww. ja v  a  2 s .com

    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, authToken.username));
    params.add(new BasicNameValuePair(PARAM_PASSWORD, authToken.password));
    params.add(new BasicNameValuePair(PARAM_LOGINTYPE, PARAM_LOGINTYPE_VALUE_LOGIN));
    final HttpEntity entity;
    try {
        entity = new UrlEncodedFormEntity(params);
    } catch (final UnsupportedEncodingException e) {
        // this should never happen.
        throw new IllegalStateException(e);
    }

    HttpPost post = new HttpPost(LOGIN_URL);
    post.addHeader(entity.getContentType());
    post.setHeader("Accept", "*/*");
    post.setEntity(entity);

    HttpResponse resp = httpClient.execute(post);
    if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new AuthenticationException("Invalid credentials!");
    }

    String response = EntityUtils.toString(resp.getEntity());

    Document doc = Jsoup.parse(response, BASE_URL);

    Element selectora = doc.getElementById("selectora");
    if (selectora == null) {
        throw new ParseException("can't find #selectora");
    }
    Element form = selectora.getElementById("formPageSelector");
    if (form == null) {
        throw new ParseException("can't find #formPageSelector");
    }
    Element input = form.getElementById("LogSesID");
    if (input == null) {
        throw new ParseException("can't find #LogSesID");
    }

    authToken.sessionId = input.attr("value");
}

From source file:eu.masconsult.bgbanking.banks.fibank.my.MyFIBankClient.java

@Override
public List<RawBankAccount> getBankAccounts(String authtoken)
        throws IOException, ParseException, AuthenticationException {
    Log.v(TAG, "getBankAccounts: " + authtoken);
    AuthToken authToken = AuthToken.fromJson(authtoken);
    DefaultHttpClient httpClient = getHttpClient();

    performLogin(httpClient, authToken);

    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("request_type", "open"));
    params.add(new BasicNameValuePair("open_tab", "home"));
    params.add(new BasicNameValuePair("LogSesID", authToken.sessionId));
    final HttpEntity entity;
    try {//from w  w w  .  j  a v  a  2  s. c o  m
        entity = new UrlEncodedFormEntity(params);
    } catch (final UnsupportedEncodingException e) {
        // this should never happen.
        throw new IllegalStateException(e);
    }

    HttpPost post = new HttpPost(SUMMARY_URL);
    post.addHeader(entity.getContentType());
    post.setHeader("Accept", "*/*");
    post.setEntity(entity);
    /*
     * curl -b 'ASP.NET_SessionId=afmrm5b0eiesmhha14ml2xml' -d
     * request_type=open -d open_tab=home -d
     * LogSesID=80e46fac-e188-4055-93de-137bac9db9a3
     * https://my.fibank.bg/lAccSummary
     */

    HttpResponse resp = httpClient.execute(post);

    if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new ParseException("getBankAccounts: unhandled http status "
                + resp.getStatusLine().getStatusCode() + " " + resp.getStatusLine().getReasonPhrase());
    }

    String response = EntityUtils.toString(resp.getEntity());
    Log.v(TAG, "response = " + response);

    Document doc = Jsoup.parse(response, BASE_URL);

    Element table = doc.getElementById("AvailableAmt");
    if (table == null) {
        throw new ParseException("can't find @AvailableAmt");
    }

    List<RawBankAccount> bankAccounts = new LinkedList<RawBankAccount>();
    for (Element row : table.getElementsByTag("tr")) {
        RawBankAccount bankAccount = obtainBankAccountFromHtmlTableRow(row);
        if (bankAccount != null) {
            bankAccounts.add(bankAccount);
        }
    }

    return bankAccounts;
}

From source file:eu.masconsult.bgbanking.banks.dskbank.DskClient.java

@Override
public String authenticate(String username, String password)
        throws IOException, ParseException, CaptchaException {
    final HttpResponse resp;
    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, username));
    params.add(new BasicNameValuePair(PARAM_PASSWORD, password));
    final HttpEntity entity;
    try {/*from w w  w  . jav  a  2 s  .co m*/
        entity = new UrlEncodedFormEntity(params);
    } catch (final UnsupportedEncodingException e) {
        // this should never happen.
        throw new IllegalStateException(e);
    }
    String uri = BASE_URL + "?"
            + URLEncodedUtils.format(Arrays.asList(new BasicNameValuePair(XML_ID, AUTH_XML_ID)), ENCODING);
    Log.i(TAG, "Authenticating to: " + uri);
    final HttpPost post = new HttpPost(uri);
    post.addHeader(entity.getContentType());
    post.setHeader("Accept", "*/*");
    post.setEntity(entity);
    try {
        resp = getHttpClient().execute(post);

        if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new ParseException("login: unhandled http status " + resp.getStatusLine().getStatusCode()
                    + " " + resp.getStatusLine().getReasonPhrase());
        }

        String response = EntityUtils.toString(resp.getEntity());
        Log.v(TAG, "response = " + response);

        Document doc = Jsoup.parse(response, BASE_URL);
        Element mainForm = doc.getElementById("mainForm");
        if (mainForm == null) {
            throw new ParseException("login: missing mainForm");
        }

        String action = BASE_URL + mainForm.attr("action");
        Log.v(TAG, "action=" + action);
        UrlQuerySanitizer sanitizer = new UrlQuerySanitizer(action);
        String user_id = sanitizer.getValue(PARAM_USER_ID);
        String session_id = sanitizer.getValue(PARAM_SESSION_ID);

        if (user_id == null || "".equals(user_id) || session_id == null || "".equals(session_id)) {
            if (doc.getElementsByClass("redtext").size() > 0) {
                // bad authentication
                return null;
            } else {
                // TODO handle captcha
                Elements captcha = doc.select("input[name=captcha_hkey]");
                if (captcha != null && captcha.size() == 1) {
                    String captchaHash = captcha.first().attr("value");
                    String captchaUri = BASE_URL + "?"
                            + URLEncodedUtils
                                    .format(Arrays.asList(new BasicNameValuePair(XML_ID, CAPTCHA_XML_ID),
                                            new BasicNameValuePair("captcha_key", captchaHash)), ENCODING);
                    throw new CaptchaException(captchaUri);
                }
                throw new ParseException("no user_id or session_id: " + action);
            }
        }

        return URLEncodedUtils.format(Arrays.asList(new BasicNameValuePair(PARAM_USER_ID, user_id),
                new BasicNameValuePair(PARAM_SESSION_ID, session_id)), ENCODING);
    } catch (ClientProtocolException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:nl.phanos.liteliveresultsclient.LoginHandler.java

public List<NameValuePair> getFormParams(String html, String username, String password)
        throws UnsupportedEncodingException {

    Document doc = Jsoup.parse(html);

    // Google form id
    Element loginform = doc.getElementById("primarycontent");
    Elements inputElements = loginform.getElementsByTag("input");

    List<NameValuePair> paramList = new ArrayList<NameValuePair>();

    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        if (key.equals("email")) {
            value = username;/*  ww  w.j a va 2  s  .  c  om*/
        } else if (key.equals("password")) {
            value = password;
        }

        paramList.add(new BasicNameValuePair(key, value));

    }

    return paramList;
}

From source file:ru.org.linux.user.EditRegisterWebTest.java

/**
 *       . ?   /*from ww w .  ja  va2s . c o m*/
 * redirect  
 * @throws IOException
 */
@Test
public void testSimple() throws IOException {
    String auth = WebHelper.doLogin(resource, "JB", JB_PASS);

    ClientResponse cr = resource.path("people/JB/edit")
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, auth, "/", "127.0.0.1", 1)).get(ClientResponse.class);

    assertEquals(HttpStatus.SC_OK, cr.getStatus());

    Document doc = Jsoup.parse(cr.getEntityInputStream(), "UTF-8", resource.getURI().toString());

    assertEquals("/people/JB/edit", doc.getElementById("editRegForm").attr("action"));

    String name = doc.getElementById("name").val();
    String url = doc.getElementById("url").val();
    String email = doc.getElementById("email").val();
    String town = doc.getElementById("town").val();
    String info = doc.getElementById("info").val();

    assertEquals(JB_NAME, name);
    assertEquals(JB_URL, url);
    assertEquals(JB_EMAIL, email);
    assertEquals(JB_TOWN, town);
    assertEquals(JB_INFO, info);

    MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("name", name);
    formData.add("url", url);
    formData.add("email", email);
    formData.add("town", town);
    formData.add("info", info);
    formData.add("csrf", "csrf");
    formData.add("oldpass", JB_PASS);

    ClientResponse cr2 = resource.path("people/maxcom/edit")
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, auth, "/", "127.0.0.1", 1))
            .cookie(new Cookie(CSRFProtectionService.CSRF_COOKIE, "csrf")).post(ClientResponse.class, formData);

    assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, cr2.getStatus());
    assertEquals("http://127.0.0.1:8080/people/JB/profile", cr2.getLocation().toString());
}

From source file:ru.org.linux.user.EditRegisterWebTest.java

@Test
public void testChange() throws IOException {
    String auth = WebHelper.doLogin(resource, "JB", JB_PASS);

    ClientResponse cr = resource.path("people/JB/edit")
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, auth, "/", "127.0.0.1", 1)).get(ClientResponse.class);

    assertEquals(HttpStatus.SC_OK, cr.getStatus());

    Document doc = Jsoup.parse(cr.getEntityInputStream(), "UTF-8", resource.getURI().toString());

    assertEquals("/people/JB/edit", doc.getElementById("editRegForm").attr("action"));

    String name = doc.getElementById("name").val();
    String url = doc.getElementById("url").val();
    String email = doc.getElementById("email").val();
    String town = doc.getElementById("town").val();
    String info = doc.getElementById("info").val();

    assertEquals(JB_NAME, name);// w  w  w .  j a  va2 s  .  com
    assertEquals(JB_URL, url);
    assertEquals(JB_EMAIL, email);
    assertEquals(JB_TOWN, town);
    assertEquals(JB_INFO, info);

    MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("name", name);
    formData.add("url", url);
    formData.add("email", email);
    formData.add("town", town);
    formData.add("info", info);
    formData.add("csrf", "csrf");

    ClientResponse cr2 = resource.path("people/JB/edit")
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, auth, "/", "127.0.0.1", 1))
            .cookie(new Cookie(CSRFProtectionService.CSRF_COOKIE, "csrf")).post(ClientResponse.class, formData);

    Document doc2 = Jsoup.parse(cr2.getEntityInputStream(), "UTF-8", resource.getURI().toString());

    assertEquals(HttpStatus.SC_OK, cr2.getStatus());
    assertEquals("? ? ?   ",
            doc2.select(".error").text());
    assertEquals("/people/JB/edit", doc2.getElementById("editRegForm").attr("action"));
}

From source file:eu.masconsult.bgbanking.banks.sgexpress.SGExpressClient.java

@Override
public List<RawBankAccount> getBankAccounts(String authTokenString)
        throws IOException, ParseException, AuthenticationException {
    AuthToken authToken = AuthToken.fromJson(authTokenString);

    String response = loadPageWithAuth(getHttpClient(), authToken, LIST_ACCOUNTS_XML_ID);

    Document doc = Jsoup.parse(response, BASE_URL);

    Element content = doc.getElementById("main");
    if (content == null) {
        throw new ParseException("getBankAccounts: can't find #main");
    }/*ww w .j  ava2 s .  co m*/

    Elements tables = content.select("section.result table.data");
    if (tables == null || tables.size() == 0) {
        throw new ParseException("getBankAccounts: can't find table section.result table.data");
    }

    Elements rows = tables.first().getElementsByTag("tr");
    if (rows == null || rows.size() == 0) {
        throw new ParseException("getBankAccounts: first table is empty");
    }

    ArrayList<RawBankAccount> bankAccounts = new ArrayList<RawBankAccount>(rows.size());

    String type = "undef";
    for (Element row : rows) {
        if (row.getElementsByTag("th").size() > 0) {
            // header row
            type = row.child(0).text();
        } else {
            RawBankAccount bankAccount = obtainBankAccountFromHtmlTableRow(type, row);
            if (bankAccount != null) {
                bankAccounts.add(bankAccount);
            }
        }
    }

    return bankAccounts;
}

From source file:ru.org.linux.user.EditRegisterWebTest.java

@Test
public void testChangePassword() throws IOException {
    String auth = WebHelper.doLogin(resource, "maxcom", MAXCOM_PASS);
    ClientResponse cr = resource.path("people/maxcom/edit")
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, auth, "/", "127.0.0.1", 1)).get(ClientResponse.class);

    assertEquals(HttpStatus.SC_OK, cr.getStatus());

    Document doc = Jsoup.parse(cr.getEntityInputStream(), "UTF-8", resource.getURI().toString());

    String name = doc.getElementById("name").val();
    String url = doc.getElementById("url").val();
    String email = doc.getElementById("email").val();
    String town = doc.getElementById("town").val();
    String info = doc.getElementById("info").val();

    assertEquals(MAXCOM_NAME, name);/*  w w w.j  a  va 2s  .c  om*/
    assertEquals(MAXCOM_URL, url);
    assertEquals(MAXCOM_EMAIL, email);
    assertEquals(MAXCOM_TOWN, town);
    assertEquals(MAXCOM_INFO, info);

    MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
    formData.add("name", name);
    formData.add("url", url);
    formData.add("email", email);
    formData.add("town", town);
    formData.add("info", info);
    formData.add("csrf", "csrf");
    formData.add("oldpass", "passwd");
    formData.add("password", "passwd2");
    formData.add("password2", "passwd2");

    ClientResponse cr2 = resource.path("people/maxcom/edit")
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, auth, "/", "127.0.0.1", 1))
            .cookie(new Cookie(CSRFProtectionService.CSRF_COOKIE, "csrf")).post(ClientResponse.class, formData);

    assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, cr2.getStatus());

    String newAuth = WebHelper.getAuthCookie(cr2);

    assertNotNull(newAuth);

    ClientResponse cr3 = resource.uri(cr2.getLocation())
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, newAuth, "/", "127.0.0.1", 1)).get(ClientResponse.class);

    assertEquals(HttpStatus.SC_OK, cr3.getStatus());

    MultivaluedMap<String, String> formData2 = new MultivaluedMapImpl();
    formData2.add("name", name);
    formData2.add("url", url);
    formData2.add("email", email);
    formData2.add("town", town);
    formData2.add("info", info);
    formData2.add("csrf", "csrf");
    formData2.add("oldpass", "passwd2");
    formData2.add("password", "passwd");
    formData2.add("password2", "passwd");

    ClientResponse cr4 = resource.path("people/maxcom/edit")
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, newAuth, "/", "127.0.0.1", 1))
            .cookie(new Cookie(CSRFProtectionService.CSRF_COOKIE, "csrf"))
            .post(ClientResponse.class, formData2);

    assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, cr4.getStatus());

    String newAuth2 = WebHelper.getAuthCookie(cr4);

    ClientResponse cr5 = resource.uri(cr4.getLocation())
            .cookie(new Cookie(WebHelper.AUTH_COOKIE, newAuth2, "/", "127.0.0.1", 1)).get(ClientResponse.class);

    assertEquals(HttpStatus.SC_OK, cr5.getStatus());
}

From source file:info.dolezel.fatrat.plugins.UloztoDownload.java

@Override
public void processLink(String link) {

    //if (link.contains("/live/"))
    //    link = link.replace("/live/", "/");
    if (link.startsWith("http://uloz.to") || link.startsWith("https://uloz.to"))
        link = link.replace("https?://uloz.to", "https://www.uloz.to");
    if (link.startsWith("http://m.uloz.to") || link.startsWith("https://m.uloz.to"))
        link = link.replace("https?://m.uloz.to", "https://www.uloz.to");

    if (!logIn(link))
        return;/* w w w  .  j a  va 2s .c  o  m*/

    final String downloadLink = link; // I can't make 'link' final

    fetchPage(link, new PageFetchListener() {

        @Override
        public void onCompleted(ByteBuffer buf, Map<String, String> headers) {
            try {
                if (headers.containsKey("location")) {
                    String location = headers.get("location");
                    if (location.contains("smazano") || location.contains("nenalezeno"))
                        setFailed("The file has been removed");
                    else
                        processLink(location);
                    return;
                }

                CharBuffer cb = charsetUtf8.decode(buf);

                if (cb.toString().contains("?disclaimer=1")) {
                    processLink(downloadLink + "?disclaimer=1");
                    return;
                }

                final Document doc = Jsoup.parse(cb.toString());
                final Element freeForm = doc.getElementById("frm-download-freeDownloadTab-freeDownloadForm");
                final Element premiumLink = doc.getElementById("#quickDownloadButton");

                boolean usePremium = usePremium(downloadLink);

                if (cb.toString().contains("Nem dostatek kreditu"))
                    setMessage("Credit depleted, using FREE download");
                else if (usePremium && premiumLink != null) {
                    String msg = "Using premium download";

                    Elements aCredits = doc.getElementsByAttributeValue("href", "/kredit");

                    if (!aCredits.isEmpty())
                        msg += " (" + aCredits.get(0).ownText() + " left)";

                    setMessage(msg);

                    startDownload("http://www.uloz.to" + premiumLink.attr("href"));
                    return;

                } else if (loggedIn)
                    setMessage("Login failed, using FREE download");

                Elements aNames = doc.getElementsByClass("jsShowDownload");
                if (!aNames.isEmpty())
                    reportFileName(aNames.get(0).ownText());

                final PostQuery pq = new PostQuery();
                final Map<String, String> hdr = new HashMap<String, String>();
                Elements eHiddens = freeForm.select("input[type=hidden]");

                hdr.put("X-Requested-With", "XMLHttpRequest");
                hdr.put("Referer", downloadLink);
                hdr.put("Accept", "application/json, text/javascript, */*; q=0.01");

                for (Element e : eHiddens)
                    pq.add(e.attr("name"), e.attr("value"));

                fetchPage("https://uloz.to/reloadXapca.php?rnd=" + Math.abs(new Random().nextInt()),
                        new PageFetchListener() {

                            @Override
                            public void onCompleted(ByteBuffer buf, Map<String, String> headers) {
                                CharBuffer cb = charsetUtf8.decode(buf);
                                String captchaUrl;

                                try {
                                    JSONObject json = new JSONObject(cb.toString());
                                    captchaUrl = "https:" + json.getString("image");
                                    pq.add("hash", json.getString("hash"));
                                    pq.add("timestamp", "" + json.getInt("timestamp"));
                                    pq.add("salt", "" + json.getInt("salt"));
                                } catch (JSONException e) {
                                    setFailed("Error parsing captcha JSON");
                                    return;
                                }

                                solveCaptcha(captchaUrl, new CaptchaListener() {

                                    @Override
                                    public void onFailed() {
                                        setFailed("Failed to decode the captcha code");
                                    }

                                    @Override
                                    public void onSolved(String text) {

                                        String action = freeForm.attr("action");
                                        pq.add("captcha_value", text);

                                        fetchPage("https://www.uloz.to" + action, new PageFetchListener() {

                                            @Override
                                            public void onCompleted(ByteBuffer buf,
                                                    Map<String, String> headers) {
                                                try {
                                                    CharBuffer cb = charsetUtf8.decode(buf);
                                                    JSONObject obj = new JSONObject(cb.toString());

                                                    startDownload(obj.getString("url"));
                                                } catch (Exception e) {
                                                    setFailed("" + e);
                                                }
                                            }

                                            @Override
                                            public void onFailed(String error) {
                                                setFailed(error);
                                            }

                                        }, pq.toString(), hdr);

                                    }
                                });
                            }

                            @Override
                            public void onFailed(String error) {
                                setFailed("Failed to load captcha AJAX page");
                            }

                        });

            } catch (Exception e) {
                e.printStackTrace();
                setFailed(e.toString());
            }
        }

        @Override
        public void onFailed(String error) {
            setFailed("Failed to load the initial page");
        }
    }, null);
}