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

9
我只是创建了这个应用程序:
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());

}
} 

现在我想要做的是在页面上显示的唯一按钮上进行程序化点击,但我真的不知道如何操作。我已经阅读了这里的每篇帖子,但没有人能帮助我。
这是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>
5个回答

7
如果您的按钮在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>

哇,你真是我的英雄。最后一件事,我不懂JavaScript,那我应该在function(){...}里放什么呢? - Alessio Trecani
你想让你的按钮点击做什么? - MHP
与按钮已经发布的相同。我只是想模拟用户的点击。(我觉得我说话像个孩子,但我在这方面很菜) - Alessio Trecani
1
你想将表单提交到https://www.paygol.com/pay。所以我建议你查看这个链接https://dev59.com/3HVC5IYBdhLWcg3w9GLM#133997。 - MHP
谢谢你的帮助。最后一件事是我的表单ID。我没有表单ID。无论如何,再次感谢你,你很好。 (也许我可以添加ID) - Alessio Trecani
显示剩余2条评论

7
如果您想在HTML页面上点击按钮,例如Adfly或其他网站的跳过按钮,请使用以下代码:webview.loadUrl("javascript:document.getElementById('skip_button').click()");

3

我找出了问题所在。在调用另一个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()");
        }
    });

}

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

我认为模拟用户点击是不可能的(至少出于安全原因,它不应该被允许,想象一下强制用户点击可疑广告、外部链接或甚至应用内购买按钮的情况...)

你可以做的是找出按钮点击到底做了什么并绕过它!

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

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


我认为按钮的功能在HTML页面中已经写明了。我不太懂HTML,所以才在这里发布了它。我在其他帖子中看到一些用户通过编程方式(类似于机器人)点击按钮,所以我确定这是可能的。 - Alessio Trecani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接