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

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • javascript is not something that you add in your `webView.loadUrl()`.. hmm.. [check out this sample](http://www.youtube.com/watch_popup?v=uVqp1zcMfbE&vq=hd720#t=68).... this will give u an idea to move ahead.. – CRUSADER Jun 25 '13 at 12:42

2 Answers2

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

Community
  • 1
  • 1
AlexBcn
  • 2,450
  • 2
  • 17
  • 28
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);
}
Azad
  • 5,144
  • 4
  • 28
  • 56
Peter Lo
  • 110
  • 1
  • 7