Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - fill form programmatically in Android Webview - JavaScript

I'm trying to automatically fill a form from the website of my school. I've seen some ways to do with javascrip.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navegador_siiau_layout);

    navegador_siiau = (WebView) findViewById(R.id.wv_navegador_siiau);
    navegador_siiau.getSettings().setJavaScriptEnabled(true);
    navegador_siiau.loadUrl("http://siiauescolar.siiau.udg.mx/wus/gupprincipal.inicio");
    navegador_siiau.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            String user="207512134";
            String pwd="FENMAA";
            view.loadUrl("javascript:document.getElementsByName('p_codigo_c').value = '"+user+"';document.getElementsByName('p_clave_c').value='"+pwd+"';");
        }
    });



}

The result is a blank page with the last string I try to put in the form... What can I do to fill and submit the form correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is untested, but I see two things wrong here:

  1. You should call setWebViewClient with your WebViewClient implementation before you call loadUrl. However, loadUrl is asynchronous, so it probably would not make a difference, but this is worth a try I think.

  2. You are not calling super.onPageFinished(view, url); in onPageFinshed. You should add this call.

EDIT 2:

I finally took a look at the actual page you are working with. The problem is that the page loads content in a different frame, so you actually have to make the getElementsByName call on a different document. The frame in which both of the inputs you want are located has the name mainFrame in your page. So, you should write the javascript like this and load that into the WebView as you have done above:

window.frames["mainFrame"].document.
                 getElementsByName('p_codigo_c')[0].value = "user";
window.frames["mainFrame"].document.
                 getElementsByName('p_clave_c')[0].value = "password";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...