0

Good day,

I am attempting to automatically fill in some login details within a webView on an android.

So far i have the website loaded in the webview, i used:

webView.loadUrl("http://blabla");

now I need to fill in the login details. I have attempted to do so with the following code:

webView.loadUrl("javascript: {"
                    + " var y=document.getElementsByTagName('input');"
                    + " document.write('0:' + y[0].value + '<br>')"
                    + " y[0].value = '" + username + "';"
                    + " y[1].value = '" + password + "';}");

But that is not working, So i tried a simpler task to check that my javascript is working. So i ran this:

   webView.loadUrl("javascript:alert('hello');");

But even that does nothing when running it.

I have enable javascript for the webview:

    WebSettings webSet = webView.getSettings();
    webSet.setJavaScriptEnabled(true); // enable java script

What am i doing wrong? Thanks

1
  • javascript is not something that you add in your webView.loadUrl().. hmm.. check out this sample.... this will give u an idea to move ahead.. Commented Jun 25, 2013 at 12:42

2 Answers 2

0

You should execute the JS once the website is loaded on your webview. Look the example I did on this answer.

private final static String HOST = "...";
private WebView wb;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_home);
    wb = (WebView) findViewById(R.id.home_webview);
    WebSettings webSettings = wb.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wb.setWebViewClient(new WebViewClient(){
        public void onPageFinished(WebView view, String url){
                        //Inject JS code here
                        wb.loadUrl("javascript:(function(){...})()");
        }           
    });     
    wb.loadUrl("http://"+HOST);
}

onPageFinished is executed once loaded/finished

Sign up to request clarification or add additional context in comments.

Comments

0

Did you enabled the javascript and added the WebChromeClient?

Also, you should put the javascript inside WebViewClient-onPageFinished, because you want the JavaScript to be run after page loaded.

Here is the sample.

public void onCreate(Bundle savedInstanceState)    {
    super.onCreate(savedInstanceState);

    WebView wv = new WebView(this);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl("http://www.google.com");
    wv.setWebChromeClient(new WebChromeClient());
    wv.setWebViewClient(new WebViewClient(){
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            view.loadUrl("javascript:alert('hello');");
        }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    });

    setContentView(wv);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.