如何在 Webview 中执行按钮单击 Android

How to perform a button click inside a Webview Android

我只是创建了这个应用程序:

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

}
} 

现在我想做的是以编程方式单击页面上显示的唯一按钮,但我真的不知道该怎么做。我已经阅读了这里的所有 post,但没有人可以帮助我。

这是 HTML 页面:

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

我认为伪造用户点击是不可能的(至少我认为仅出于安全原因不应该被允许,想象一下强迫用户点击有问题的广告、外部链接甚至可能-app 购买按钮,...)

您可以做的是找出按钮点击的确切作用并绕过它!

1) 它会让一些 html 可见吗? -> 将网页另存为字符串并注入 javascript 以使隐藏的内容可见 2)它是否在后台加载东西(如数据列表..) - >使用Fiddler检查请求,将网页另存为字符串,伪造请求并自己注入数据 3) ...

很大程度上取决于您的按钮在做什么 ;)

如果您的 Button 在 Html 页面中,那么您可以简单地 运行 javaScript 代码来模拟这样的点击事件:

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

您还需要在 Html 页面中定义 clickFunction:

function clickFunction() {
    //click event
}

或者您也可以通过 javascript 添加以上功能:

 view.loadUrl("javascript:clickFunction(){ //click event })()"); 

更新:

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

我明白问题出在哪里了。我不得不等待页面加载后再调用另一个 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()");
        }
    });

}

如果您想点击 html 页面内的按钮,例如 adfly 或其他网站的跳过按钮。 使用此代码 webview.loadUrl("javascript:document.getElementById('skip_button').click()");

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

        }

    });