How to automatically enter a password in Webview

I am new to Java and Android and I have a question. I think the solution can be very simple, but I could not solve it.

I created the application with WebView because I want the webview to automatically enter the username and password so that the user is already logged in when the application starts. I googled and found something like this, but this does not work.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("https://euterpe.webuntis.com/WebUntis/index.do#main"); webView.setWebViewClient(new WebViewClient ()); class MyWebViewClient extends WebViewClient { @Override public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { handler.proceed("Username", "password"); } } 
+4
source share
3 answers

hi, please try the following:

 webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("javascript:document.getElementsByName('school').value = 'schoolname'"); webView.loadUrl("javascript:document.getElementsByName('j_username').value = 'username'"); webView.loadUrl("javascript:document.getElementsByName('j_password').value = 'password'"); webView.loadUrl("javascript:document.forms['login'].submit()"); 

first line includes javascript

all other lines fill out the form and, finally, submit the form.

Look at the apostrophes of String, please, I edited my answer.

0
source

Although this is an old question, I thought I would post my solution:

  webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("http://your.url"); webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { view.loadUrl("javascript:document.getElementsByName('school')[0].value = 'schoolname'"); view.loadUrl("javascript:document.getElementsByName('j_username')[0].value = 'username'"); view.loadUrl("javascript:document.getElementsByName('j_password')[0].value = 'password'"); view.loadUrl("javascript:document.forms['login'].submit()"); } }); 

This is similar to the answer from Benjamin. The only difference is part [0] , selecting the first element found, plus my implementation expects the page to load before javascript is run on it.

0
source

Replace

 webView.setWebViewClient(new WebViewClient ()); 

with

 webView.setWebViewClient(new MyWebViewClient ()); 

Only then will the onReceivedHttpAuthRequest override method be called.

This processing will only work for HTTP authentication requests.

0
source

All Articles