zoukankan      html  css  js  c++  java
  • HtmlUnit: A Simple Example: Check Yahoo Email---转载

    1. src http://www.muneebahmad.com/index.php/archives/81

    package com.examples.htmlunit;
    
    import java.io.IOException;
    import java.net.URL;
    import java.util.List;
    
    import com.gargoylesoftware.htmlunit.BrowserVersion;
    import com.gargoylesoftware.htmlunit.Page;
    import com.gargoylesoftware.htmlunit.RefreshHandler;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
    import com.gargoylesoftware.htmlunit.html.HtmlForm;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlTable;
    import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
    
    public class YahooMail {
    
        public static void main(String[] args) throws Exception {
    
            // Create and initialize WebClient object
            WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
            webClient.setThrowExceptionOnScriptError(false);
            webClient.setRefreshHandler(new RefreshHandler() {
                public void handleRefresh(Page page, URL url, int arg) throws IOException {
                    System.out.println("handleRefresh");
                }
    
            });
    
            // visit Yahoo Mail login page and get the Form object
            HtmlPage page = (HtmlPage) webClient.getPage("https://login.yahoo.com/config/login_verify2?.intl=us&.src=ym");
            HtmlForm form = page.getFormByName("login_form");
    
            // Enter login and passwd
            form.getInputByName("login").setValueAttribute("@@@@@@@");
            form.getInputByName("passwd").setValueAttribute("@@@@@@@");
    
            // Click "Sign In" button/link
            page = (HtmlPage) form.getInputByValue("Sign In").click();
    
            // Click "Inbox" link
            HtmlAnchor anchor = (HtmlAnchor) page.getHtmlElementById("WelcomeInboxFolderLink");
            page = (HtmlPage) anchor.click();
    
            // Get the table object containing the mails
            HtmlTable dataTable = (HtmlTable) page.getHtmlElementById("datatable");
    
            // Go through each row and count the row with class=msgnew
            int newMessageCount = 0;
            List rows = (List) dataTable.getHtmlElementsByTagName("tr");
            for (HtmlTableRow row: rows) {
                if (row.getAttribute("class").equals("msgnew")) {
                    newMessageCount++;
                }
            }        
    
            // Print the newMessageCount to screen
            System.out.println("newMessageCount = " + newMessageCount);
    
            //System.out.println(page.asXml());                        
    
        }
  • 相关阅读:
    struts2文件上传报错
    简述算法和程序的区别并举例说明
    JAVA中TreeMap集合筛选字母及每一个字符出现的次数
    Myeclipse2014破解步骤
    修改ubuntu的终端提示符
    gcc 引用math.h头文件,编译出现undefined reference to `pow‘等错误时,需要加参数lm.
    几篇文章
    gdb调试gcc出现:Missing separate debuginfos, use: debuginfoinstall glibcx.i686
    【达内C++学习培训学习笔记系列】C语言之三循环语句和数组
    code::block之spell checker配置
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3448240.html
Copyright © 2011-2022 走看看