7

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>
Alessio Trecani
  • 673
  • 2
  • 10
  • 21

5 Answers5

6

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

user8307049
  • 61
  • 1
  • 2
4

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>
MHP
  • 2,385
  • 1
  • 13
  • 25
  • Oh man you are my hero. Just last thing, I don't know javascript so what should I put inside the function(){...}?? – Alessio Trecani May 08 '15 at 08:46
  • what do you want your button click do? – MHP May 08 '15 at 08:47
  • The same thing that the button posted already does. I simply want to simulate a click from an user. ( I think I m talking like a baby but I m so noob in this) – Alessio Trecani May 08 '15 at 08:50
  • 1
    you want submit your form to https://www.paygol.com/pay. so I suggest you to see this link http://stackoverflow.com/a/133997/3553815 – MHP May 08 '15 at 08:55
  • var form = document.getElementById("your form id"); form.submit(); – MHP May 08 '15 at 08:59
  • Thanks for helping me. Last thing which is my form id. I have no form ID.Anyway thanks again you have been nice. (Probably can I add th ID) – Alessio Trecani May 08 '15 at 09:01
  • Hey bro sorry again. But it doesn't work. I insert :view.loadUrl("http://catcheat.net/test/test.html"); to connect on the page where the button is located and then view.loadUrl("javascript:clickFunction()"); – Alessio Trecani May 08 '15 at 09:23
2

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()");
        }
    });

}
Alessio Trecani
  • 673
  • 2
  • 10
  • 21
1
    // 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();})();");

        }

    });
-1

I don't think faking a users click is possible (at least I'd imagine for security reasons alone it shouldn't be allowed, imagine forcing a user to click on questionable advertisment, external links or maybe even in-app buy buttons, ...)

What you could do is figure out what exactly the button click is doing and bypass it!

1) Does it make some html visible? -> Save webpage as string and inject javascript to make the hidden stuff visible 2) Does it load stuff in the background (like a list of data..) -> Use Fiddler to inspect the request, Save webpage as string, fake the request and inject the data yourself 3) ...

Depends a lot on what your button is doing ;)

ftb
  • 59
  • 8
  • I think tath what the button does is written right in the HTML page. I don't know much of HTML and is for this that I posted it here. I ve seen in other thread that some users performed a click on button programmatically (like a sort of bot) then I m sure that is possible to do. – Alessio Trecani May 08 '15 at 08:41