How to execute button click inside android web browser
I just created this application:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) findViewById(R.id.webView);
WebSettings faller = view.getSettings();
faller.setJavaScriptEnabled(true);
view.loadUrl("http://catcheat.net/test/test.html");
view.setWebViewClient(new WebViewClient());
}
}
Now what I want to do is click programmatically on the unique button that is displayed on the page. But I really don't know how to do it. I read every post like here but nobody could help me.
This is the HTML page:
<html>
<body>
<form name="pg_frm" method="post" action="https://www.paygol.com/pay" >
<input type="hidden" name="pg_serviceid" value="333818">
<input type="hidden" name="pg_currency" value="EUR">
<input type="hidden" name="pg_name" value="Donation">
<input type="hidden" name="pg_custom" value="">
<input type="hidden" name="pg_price" value="0.5">
<input type="hidden" name="pg_return_url" value="">
<input type="hidden" name="pg_cancel_url" value="">
<input type="image" name="pg_button" src="https://www.paygol.com/webapps /buttons/en/white.png" border="0" alt="Make payments with PayGol: the easiest way!" title="Make payments with PayGol: the easiest way!" >
</form>
</body>
</html>
if your button is in your Html page, so you can just run javaScript code to simulate the click event like this:
view.loadUrl("javascript:clickFunction()");
also you need to define clickFunction in the Html page:
function clickFunction() {
//click event
}
or you can add the function above with javascript:
view.loadUrl("javascript:clickFunction(){ //click event })()");
UPDATE:
<html>
<head>
<script>
function clickFunction(){
var form = document.getElementById("myform");
form.submit();
}
</script>
</head>
<body>
<form id="myform" name="pg_frm" method="post" action="https://www.paygol.com/pay" >
<input type="hidden" name="pg_serviceid" value="333818">
<input type="hidden" name="pg_currency" value="EUR">
<input type="hidden" name="pg_name" value="Donation">
<input type="hidden" name="pg_custom" value="">
<input type="hidden" name="pg_price" value="0.5">
<input type="hidden" name="pg_return_url" value="">
<input type="hidden" name="pg_cancel_url" value="">
<input type="image" name="pg_button" src="https://www.paygol.com/webapps /buttons/en/white.png" border="0" alt="Make payments with PayGol: the easiest way!" title="Make payments with PayGol: the easiest way!" >
</form>
</body>
</html>
If you want to click on a button inside html page like skip as button for remote or other sites. Use this code
webview.loadUrl("javascript:document.getElementById('skip_button').click()");
I figured out what the problem is. I had to wait for the page to be loaded before calling another view.load ..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) findViewById(R.id.webView);
WebSettings faller = view.getSettings();
String url = "http://catcheat.net/test/test.html";
faller.setJavaScriptEnabled(true);
view.loadUrl(url);
view.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view , String url){
view.loadUrl("javascript:clickFunction()");
}
});
}
// important don't forget to set below line in onCreate
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// instead of wp-submit you can set your own element id
webView.loadUrl("javascript:(function(){document.getElementById('wp-submit').click();})();");
}
});
I don't think faking user clicks is possible (at least I would suggest that just for security reasons this should not be allowed, imagine to get the user to click on questionable ads, external links, or maybe even in-app buy buttons , ...)
What you can do is figure out what exactly the button click does and bypasses it!
1) Is this html visible? -> Save webpage as string and insert javascript to make hidden stuff visible 2) Does stuff in background (like data list ..) -> Use Fiddler to validate request, Save webpage as string, fake request and enter data yourself 3) ...
Depends on what your button does;)