如何在Webview中自动执行按钮单击?

时间:2015-11-07 16:24:29

标签: javascript android webview

我正在尝试在Android内自动化this网页。到目前为止,我的webview已成功填写表单,但唯一的问题是页面底部的最后一个按钮点击。这是我的代码:

    // Fill out form
    webview.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            WebView myWebView = (WebView) findViewById(R.id.webview);

            //hide loading image
            findViewById(R.id.progressBar).setVisibility(View.GONE);
            //show WebView
            findViewById(R.id.webview).setVisibility(View.VISIBLE);

            myWebView.loadUrl("javascript:document.getElementById('join_first_name').value='" + name + "';void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('join_last_name').value='" + lastName + "';void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('join_email').value='" + email + "';void(0);");
            myWebView.loadUrl("javascript:document.getElementById('join_confirm_email').value='" + email + "';void(0);");
            myWebView.loadUrl("javascript:document.getElementById('join_password').value='" + password + "';void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('join_confirm_password').value='" + password + "';void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('phone_number_a').value='231';void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('phone_number_b').value='123';void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('phone_number_c').value='2310';void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('join_i_agree').click();void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('join_card_not_available').click();void(0); ");
            myWebView.loadUrl("javascript:document.getElementById('#join-now-primary')[1];void(0); ");

            myWebView.pageDown(true);
            // Make sure a toast is only shown once.
            while (toastCheck) {
                Toast.makeText(MainActivity.this, "Click the \"join\" button.", Toast.LENGTH_SHORT).show();
                toastCheck = false;
            }
        }
    });

我的问题是:如何点击页面底部的按钮?

1 个答案:

答案 0 :(得分:0)

HTML 4.01 specification表示ID必须是文档范围内唯一的。

HTML 5 specification说同样的话,但换句话说。它说ID在 home subtree 中必须是唯一的,如果我们read the definition of it,它基本上就是文档。

来源:https://softwareengineering.stackexchange.com/questions/127178/two-html-elements-with-same-id-attribute-how-bad-is-it-really

我猜您应该与网站开发者联系。你也可以像这样触发它:

javascript:document.getElementById('join-now-primary').click();

编辑:

该网站正在使用jQuery,因此您可以使用jquery函数,请再试一次:

  String injection = "javascript:"

  injection += "$('#join_first_name').val('"+lastName+"');"
  injection += "$('#join_email').val('"+email+"');"
  injection += "$('#join_confirm_email').val('"+email+"');"
  injection += "$('#join_password').val('"+password+"');"
  injection += "$('#join_confirm_password').val('"+password+"');"
  injection += "$('#phone_number_a').val('2122');"
  injection += "$('#phone_number_b').val('122');"
  injection += "$('#phone_number_c').val('122');"
  injection += "$('#join_i_agree').trigger('click');"
  injection += "$('#join_card_not_available').trigger('click');"

  /* trigger form*/
  injection += "var formElement = $('#join_now_form');"
  injection += "formHandler = new FormHandler('#join_now_form', {disableDefaultSubmision: true});"
  injection += "formHandler.clearError();"
  injection += "formHandler.init();"
  injection += "formHandler.submitHandler = function(){ formElement.trigger('submitHandler', [formHandler]);}"
  injection += "AccountModals.init(formHandler, 'registration');"

  webView.loadUrl(injection)