近来因为要搞一些swt游览器提交表单的功能,用到了Browser控件,发现Browser控件可以用Js来提交表单,但是我做了个例子,访问了淘宝的一个登录页面https://login.taobao.com/member/login.jhtml,但是提交表单Post请求时,没有反应,不知道是哪里的问题,下面给出代码,请高手指点下,谢谢!!package com.browser;import java.net.HttpURLConnection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressAdapter;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;/**
 * This class implements a web browser
 */
public class SimpleBrowser {
    private Button button;
    private Combo url;
    private Browser browser;
    private static HttpURLConnection conn = null;    
    /**
     * Runs the application
     * 
     * the initial location to display
     */
    public void run() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Advanced Browser");
        createContents(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }    /**
     * Creates the main window's contents
     * 
     * @param shell
     * the main window
     * @param location
     * the initial location
     */
    public void createContents(Shell shell) {
        shell.setLayout(new FormLayout());        Composite controls = new Composite(shell, SWT.NONE);
        FormData data = new FormData();
        browser = new Browser(shell, SWT.BORDER);
        data.top = new FormAttachment(controls);
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(100, 0);
        data.bottom = new FormAttachment(100, 0);
        browser.setLayoutData(data);        controls.setLayout(new GridLayout(7, false));        url = new Combo(controls, SWT.ARROW_DOWN);
        url.setLayoutData(new GridData(496, SWT.DEFAULT));
        url.setFocus();
        
        button = new Button(controls, SWT.PUSH);
        button.setText("Go");
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                String urlStr = url.getText();
                //访问页面
                browser.setUrl(urlStr);
                //browser.setUrl("http://www.taobao.com");
            }
        });
        browser.addProgressListener(new ProgressAdapter(){
public void completed(ProgressEvent event){
//System.out.println(browser.getUrl());
if(browser.getUrl().startsWith("http://")){
System.out.println("运行Js");
browser.execute(getGotoScript());
//browser.execute("alert(document.documentElement.outerHTML)");  //可以得到页面源文件
browser.execute(loginPageScript());
}
}
});
        shell.setDefaultButton(button);
    }
    
    private String getGotoScript(){
     //String script="document.forms[0].submit();";
String script="window.location.href = 'https://login.taobao.com/member/login.jhtml'";
return script;
}
    
    private String loginPageScript(){
     String script="var currentPage = document.documentElement.outerHTML;" +
     "document.getElementsByName('TPL_username').value = '丘列有';" +
     "alert(document.getElementsByName('TPL_username').value);" +
     "document.getElementsByName('TPL_password').value = 'abc646300';" +
     "var form = document.getElementById('J_StaticForm');" +
     "form.submit();";
return script;
}    public static void main(String[] args) {
        new SimpleBrowser().run();
    }
}

解决方案 »

  1.   

    现在已经有进展了,能提交数据登录淘宝了,但是还有问题
    第一步:登录淘宝
    第二步:访问代付款链接
    第三步:完成代付款
    第四步:关闭浏览器
    我想完成上面4步,现在我只写了前两步,当我再加上第三步的时候,就会出现先执行第三步,再执行第二步的情况,因为第二步比较慢,要检测支付宝,看下有什么办法可以按第一步,第二步,第三步,第四步这样执行下来,现在是步骤乱了。我将代码放出来,可以直接在IDE运行的,大家帮忙看下package com.browser;import java.io.File;
    import org.apache.commons.io.FileUtils;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.browser.Browser;
    import org.eclipse.swt.browser.ProgressAdapter;
    import org.eclipse.swt.browser.ProgressEvent;
    import org.eclipse.swt.layout.FormAttachment;
    import org.eclipse.swt.layout.FormData;
    import org.eclipse.swt.layout.FormLayout;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    /**
     * This class implements a web browser
     */
    public class HttpLoginTaobao {
        private Browser browser;  
        private Browser browser2;  
        private Browser browser3; 
        private Browser browser4;  
        public final static String USER_DIR = System.getProperty("user.dir") + File.separatorChar;
        /**
         * Runs the application
         * 
         * the initial location to display
         */
        public void run() {
            Display display = new Display();
            Shell shell = new Shell(display);
            shell.setText("淘宝代付款");
            createContents(shell);
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
            display.dispose();
        }    /**
         * Creates the main window's contents
         * 
         * @param shell
         * the main window
         * @param location
         * the initial location
         */
        public void createContents(Shell shell) {
            shell.setLayout(new FormLayout());        Composite controls = new Composite(shell, SWT.NONE);
            FormData data = new FormData();
            browser = new Browser(shell, SWT.BORDER);
            data.top = new FormAttachment(controls);
            data.left = new FormAttachment(0, 0);
            data.right = new FormAttachment(100, 0);
            data.bottom = new FormAttachment(100, 0);
            browser.setLayoutData(data);
            controls.setLayout(new GridLayout(0, false));
            //第一步:登录淘宝
            browser.setUrl("https://login.taobao.com/member/login.jhtml?redirect_url=http%3A%2F%2Fwww.taobao.com%2F");
            browser.addProgressListener(new ProgressAdapter(){
    public void completed(ProgressEvent event){
    //System.out.println(browser.getUrl());
    if(browser.getUrl().startsWith("https://")){
    //browser.execute("alert(document.documentElement.outerHTML)");  //可以得到页面源文件
    browser.execute(loginPageScript());
    //browser.execute(getGotoScript());
    }
    }
    });
            
            //第二步:访问代付款链接
            browser2 = new Browser(shell, SWT.BORDER);
            browser2.setLayoutData(data);
            controls.setLayout(new GridLayout(0, false));
            browser2.setUrl("https://passport.baidu.com/?reg&tpl=mn");
            browser2.addProgressListener(new ProgressAdapter(){
    public void completed(ProgressEvent event){
    //System.out.println(browser.getUrl());
    if(browser2.getUrl().startsWith("https://")){
    browser.execute(gotoPayForAnother());
    }
    }
    });
            //第三步:举个例子,第二步没完成,就已经执行第三步了
            browser4 = new Browser(shell, SWT.BORDER);
            browser4.setLayoutData(data);
            controls.setLayout(new GridLayout(0, false));
            browser4.setUrl("https://passport.baidu.com/?reg&tpl=mn");
            browser4.addProgressListener(new ProgressAdapter(){
    public void completed(ProgressEvent event){
    //System.out.println(browser.getUrl());
    if(browser4.getUrl().startsWith("https://")){
    browser.execute(gotoConfirmStep1());
    }
    }
    });
        }
        
        private String loginPageScript(){
         //内部执行script
         String scriptContent = "var inp_unameObj=document.getElementById('TPL_username_1');" + 
         "if(inp_unameObj!=null) inp_unameObj.value='慕率两';" +
         "var passwordObj=document.getElementById('TPL_password');" +
         "if(passwordObj!=null) passwordObj.value='q12345';" + 
         "var form = document.getElementById('J_StaticForm'); form.submit();";
         /*
         try{
         //System.out.println(USER_DIR + "scripts/taobao_login_url.js");
    File scriptFile=new File(USER_DIR + "scripts/taobao_login_url.js");
    if(scriptFile.exists()){
    scriptContent = FileUtils.readFileToString(scriptFile);
    //System.out.println("scriptContent=" + scriptContent);
    }
         }catch(Exception e){
         e.printStackTrace();
         }*/
    return scriptContent;
    }
        
        private String gotoPayForAnother(){
    String script="window.location.href = 'http://trade.taobao.com/trade/pay.htm?biz_order_id=63601298052309&biz_type=200&ispayforanother=true'";
    return script;
    }
        
        private String gotoConfirmStep1(){
         //内部执行script
         String scriptContent = "window.location.href = 'https://auth.alipay.com/login/index.htm'";
         /*
         try{
    File scriptFile=new File(USER_DIR + "scripts/confirm_step_1.js");
    if(scriptFile.exists()){
    scriptContent = FileUtils.readFileToString(scriptFile);
    System.out.println("scriptContent=" + scriptContent);
    }
         }catch(Exception e){
         e.printStackTrace();
         }*/
    return scriptContent;
    }    public static void main(String[] args) {
            new HttpLoginTaobao().run();
        }
    }