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

Ale*_*ani 7 android click button webview

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

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

}
} 
Run Code Online (Sandbox Code Playgroud)

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

这是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>
Run Code Online (Sandbox Code Playgroud)

MHP*_*MHP 5

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

view.loadUrl("javascript:clickFunction()"); 
Run Code Online (Sandbox Code Playgroud)

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

function clickFunction() {
    //click event
}
Run Code Online (Sandbox Code Playgroud)

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

 view.loadUrl("javascript:clickFunction(){ //click event })()"); 
Run Code Online (Sandbox Code Playgroud)

更新:

 <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>
Run Code Online (Sandbox Code Playgroud)


小智 5

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