Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a button click inside a Webview Android

I simply created this app:

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 to click programmatically in the unique button that is displayed on the page But I really don't know how to do. I ve read every post like this 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>
like image 778
Alessio Trecani Avatar asked May 07 '15 23:05

Alessio Trecani


3 Answers

if your Button is in your Html page so you can simply run javaScript code to simulate click event like this:

view.loadUrl("javascript:clickFunction()"); 

also you need to define clickFunction in your Html page:

function clickFunction() {
    //click event
}

or you can add above function by javascript too:

 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>
like image 147
MHP Avatar answered Nov 20 '22 05:11

MHP


If you want to click on a button inside html page like skip as button for adfly or other sites. Use this code webview.loadUrl("javascript:document.getElementById('skip_button').click()");

like image 6
user8307049 Avatar answered Nov 20 '22 06:11

user8307049


I figured out what the problem was. I had to wait that the page was loaded before to call 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()");
        }
    });

}
like image 3
Alessio Trecani Avatar answered Nov 20 '22 07:11

Alessio Trecani