1. 程式人生 > >Webview頁面中按鈕實現自動點選

Webview頁面中按鈕實現自動點選

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebview = (WebView)findViewById(R.id.webview);
        
        WebSettings webSettings = mWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebview.setWebViewClient(new WebViewClient(){
        	@Override
        	public boolean shouldOverrideUrlLoading(WebView view, String url) {
        		// TODO Auto-generated method stub
        		Log.e("info", "login url"+url);
        		return super.shouldOverrideUrlLoading(view, url);
        	}
        	
        	@Override
        	public void onPageFinished(WebView view, String url) {
        		// TODO Auto-generated method stub
        		mWebview.loadUrl("javascript:(function(){" +
        				"var objs = document.getElementById(\"u\");" +
        				"var objs1 = document.getElementById(\"p\"); " +
        				"var objs2 = document.getElementById(\"go\"); " +
    					" objs.value = \"1471841726\";"+
    					" objs1.value= \"123456\";"+
//    					" objs2.setAttribute(\"class\", \"pass-button-full\");" +
//    					" objs2.disabled=\"\";"+
    					" objs2.click(); "+
        				"})()");
        		super.onPageFinished(view, url);
        	}
        	
        	@Override
        	public void onReceivedHttpAuthRequest(WebView view,
        			HttpAuthHandler handler, String host, String realm) {
        		// TODO Auto-generated method stub
        		Log.e("info", "onReceivedHttpAuthRequest");
        		super.onReceivedHttpAuthRequest(view, handler, host, realm);
        	}
        	
        	@SuppressLint("NewApi") @Override
        	public void onReceivedLoginRequest(WebView view, String realm,
        			String account, String args) {
        		// TODO Auto-generated method stub
        		Log.e("info", "onReceivedLoginRequest");
        		super.onReceivedLoginRequest(view, realm, account, args);
        	}
        	
        	@Override
        	public WebResourceResponse shouldInterceptRequest(WebView view,
        			WebResourceRequest request) {
        		// TODO Auto-generated method stub
//        		Log.e("info", "shouldInterceptRequest");
//        		if(request.getUrl().getPath().equals("/wp/api/login")){
//        			Log.e("info", "shouldInterceptRequest  login post send");
//        			try {
//        				handler.post(new  Runnable() {
//							public void run() {
//		                		mWebview.loadUrl("javascript:(function(){" +
//		                				"var objs = document.getElementById(\"login-username\");" +
//		                				"var objs1 = document.getElementById(\"login-password\"); " +
////		            					"alert(\"name:\"+objs.value);"+
//		                				" prompt(objs.value + \"::::\" + objs1.value);" +
//		                				"})()");
//							}
//						});
//					} catch (Exception e) {
//						Log.e("info", e.getMessage());
//						// TODO: handle exception
//					}
//        		}

        		return super.shouldInterceptRequest(view, request);
        	}
        });
        mWebview.setWebChromeClient(new WebChromeClient(){
        	
        	@Override
        	public boolean onJsAlert(WebView view, String url, String message,
        			JsResult result) {
        		// TODO Auto-generated method stub
        		Log.e("info", "onJsAlert message:"+message);
        		return super.onJsAlert(view, url, message, result);
        	}
        	
        	@Override
        	public boolean onJsConfirm(WebView view, String url,
        			String message, JsResult result) {
        		// TODO Auto-generated method stub
        		Log.e("info", "onJsConfirm url"+url);
        		return super.onJsConfirm(view, url, message, result);
        	}
        	
        	@Override
        	public boolean onJsPrompt(WebView view, String url, String message,
        			String defaultValue, JsPromptResult result) {
        		// TODO Auto-generated method stub
        		Log.e("info", "onJsPrompt message:"+message);
        		return super.onJsPrompt(view, url, message, defaultValue, result);
        	}
        	
        
        });
        
//        mWebview.loadUrl("http://wappass.baidu.com/passport/?login&tpl=wimn&ssid%3D0%26amp%3Bfrom%3D1013672a%26amp%3Buid%3D%26amp%3Bpu%3Dsz%2540320_1004%252Cta%2540iphone_2_6.0_11_8.3%26amp%3Bbd_page_type%3D1&tn=&regtype=1&u=https%3A%2F%2Fm.baidu.com");
        mWebview.loadUrl("http://ui.ptlogin2.qq.com/cgi-bin/login?style=9&appid=1600000108&low_login=0&daid=391&hln_css=http://wenwen.gtimg.cn/images/logo/sgww_logo_login_2.png&pt_no_onekey=0&s_url=http%3A%2F%2Fwenwen.m.sogou.com%2Fmu%2Fuser%3Fch%3Dwap3.in.my");
    }