Using Javascript in webview

So, reading around, I read that I can “embed” javascript on a loaded web page to program the filling out of the website form, so I tried the following:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView WebView; WebView = (WebView) findViewById(R.id.webview); WebView.setWebViewClient(new HelloWebViewClient()); WebView.getSettings().setJavaScriptEnabled(true); WebView.getSettings().setBlockNetworkImage(false); WebView.loadUrl("http://www.kingsage.es"); String username = "XXX"; String Password = "YYY"; WebView.loadUrl("javascript:document.getElementById('login_player').value='"+username+"';javascript:document.getElementById('login_passwd').value = '"+Password+"';"); } 

However, when I run this on the emulator, I get an error message as if the website was not found. if I disable the second loadUrl method, I load the website, but I need to fill out the login form programmatically !, I believe that I am doing something wrong, but I did not find and did not read. Any help would be appreciated!

+4
source share
3 answers

you need to wait for the web page to load (use WebViewClient ), then execute javascript

 webView.setWebViewClient(new WebViewClient(){ @Override public void onPageFinished (WebView webView, String url) { webView.loadUrl("javascript:document.getElementById('login_player').value='"+username+"';javascript:document.getElementById('login_passwd').value = '"+Password+"';"); } }); 

make sure that you execute only once and not on every onPageFinished Event

+6
source

I like JeanLuc's suggestion and it helped me 98%. I still needed to automatically register the user on the web page after entering the username and password, so I did the following and worked well, except for trying to delete the message “Do you want to remember the password”.

 @Override public void onPageFinished (WebView webView, String url) { webView.loadUrl("javascript:document.getElementById('Login1_UserName').value='"+userName+"';javascript:document.getElementById('Login1_Password').value = '"+password+"';"); webView.loadUrl("javascript:document.getElementById('Login1_LoginButton').click();"); } 

The second line selected the "Login" button and automatically logged in. Hope this helps someone else.

+1
source

This is how I got it in my scenario where I had to browse a page, fill in text fields and send.

 WebView webView = FindViewById<WebView>(Resource.Id.webEspView); webView.SetWebViewClient(new Client()); webView.Settings.JavaScriptEnabled = true; webView.LoadUrl("http://192.168.4.1") 

Then the Client class is added and overloaded with the onPageFinished () method.

 public class Client : WebViewClient { public override void OnPageFinished(WebView view, string url) { base.OnPageFinished(view, url); string ssid = Variables.GetInitialUsername(); ssid = ssid.Trim(new char[] {'\"'}); string pass = Variables.GetInitialPassword(); var script = $"document.getElementById(\"clientId\").value = \"{ssid}\";" + $"document.getElementById(\"branchId\").value = \"{pass}\";" + "document.querySelector('button[type=\"submit\"]').click()"; view.LoadUrl($"javascript: {script}"); } } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1392206/


All Articles