Fill Fields in Webview Automatically

Fill fields in webview automatically

You don't need to use "java commands"... but instead JavaScript... for instance:

String username = "cristian";
webview.loadUrl("javascript:document.getElementById('username').value = '"+username+"';");

So basically, what you have to do is a big string of JavaScript code that will get those fields and put values on them; also, you can enable/disable the submit button from JavaScript.

How to automatically fill the fields of a form in a webview?

You need to wait the entire web page loads, including its content (id="paypalAccountData_firstName")
So you should change

view.loadUrl("javascript:document.getElementById('paypalAccountData_firstName').value = 'test';");

to

view.loadUrl( "javascript:window.onload= (function(){ document.getElementById('paypalAccountData_firstName').value = 'test';})();");

fill form programmatically in Android Webview - JavaScript

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";

Fill form in WebView with Javascript

You should fill the values after the page has been loaded. This is an example using your code:

mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
String user="u";
String pwd="p";
view.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';");
}
});

How can I autofill a websites fields/forms in a WebView in Android Studio?

Well I still can't get facebook to work, but I got google sign in to work and some other website register/sign ins. SO basically you need to inspect the website's elements(by using google chrome on desktop) and then get the element id;

this is the code to fill out a form/field:

    webView.loadUrl("javascript:(function() { document.getElementById('last_name').value = '" + lName + "'; ;})()");

and this is the code to click a button:

    webView.loadUrl("javascript:(function() { var z = document.getElementById('signInButton').click(); })()");

Oh and I used my phone to do this, because with emulator it didn't do anything.

EDIT: I actually got facebook to kinda work by inspecting the mobile.facebook.com website, but there was no ID for email, only for password and login button and some other elements.

Fill fields WebView in Java automatically

I fixed this with JavaFX webView Javascript engine.
If anyone is intersted here's code snippet.

    String setLastName  =  "document.getElementsByName('lastName')[0].value='" + lastName + "';";
String setName = "document.getElementsByName('firstName')[0].value='" + name + "'";
String setDateBirth = "document.getElementsByName('birthdate')[0].value='" + datebirth + "';";
String setPhone = "document.getElementsByName('phone')[0].value='" + phone + "';";
String setEmail = "document.getElementsByName('email')[0].value='" + email + "';";
String setPassport = "document.getElementsByName('passport')[0].value='" + passport + "';";
Button button = new Button("Fill the form");

button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
webEngine.executeScript(setLastName);
webEngine.executeScript(setName);
webEngine.executeScript(setDateBirth);
webEngine.executeScript(setPhone);
webEngine.executeScript(setEmail);
webEngine.executeScript(setPassport);
}
});

Auto fill in fields in WebView android

loadURL is asynchronous; in other words, calling loadUrl kicks off the HTTP request, but then returns immediately, before the document is downloaded and rendered. The error you're seeing is because you're trying to execute the javascript too early (before the document is downloaded, parsed, and rendered).

You'll need to use a WebViewClient, and wait for the page to finish loading before you execute your javascript. Try the onPageFinished callback, but be careful that you can sometimes get multiple callbacks because of iFrames and the like.

Hope that helps.

EDIT
If you are targeting KITKAT and above then you also have to take care of which method to use based on android version. Follwoing code give a much better explanation:

 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
webView.evaluateJavascript(yourScript, null);
}
else
{
webView.loadUrl(yourScript);

}

Auto fill field in WebView using javascript fails

When you start to target API 19 and above, this is the behavior of WebView.loadUrl when it's passed a javascript: URL that returns a value.

You should update your app so that when running on API 19 or above, it uses evaluateJavaScript, and falls back to loadUrl when running on older devices. Something like:

if (android.os.Build.Version.SDK_INT >= 19) {
webview.evaluateJavaScript("...", null);
} else {
webview.loadUrl("javascript:...");
}

Alternatively, you could mangle your javascript into a JS function that doesn't return a value, and use that with loadUrl on all platform versions.

How to fill fields in web view in android with java and javascript

With getElementById you will get an DOM Element by id. The id AddSerialNum does not exist, this is what the error tells you.
You have an input with name="AddSerialNum", which, i think you want to get. Either add an id to it, or use getElementsByName("AddSerialNum")[0]

EDIT:
Like i already pointed out, you have to use this:

webView.loadUrl("javascript: {document.getElementsByName(\"AddSerialNum\")[0].value ='" + ABC + "';};");

The getElementsByName returns an array of elements, you have to get the first one with [0]



Related Topics



Leave a reply



Submit