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

时间:2022-08-23 22:27:49

I am trying to automate this webpage inside of Android. So far I got my webview to successfully fill out the form, but the only problem is the final button click at the bottom of the page. Here is my code:

我想在Android内自动化这个网页。到目前为止,我已经使用我的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;
            }
        }
    });

My Question is: How can I click the button at the bottom of the page?

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

1 个解决方案

#1


0  

HTML 4.01 specification says ID must be document-wide unique.

HTML 4.01规范说ID必须是文档范围内唯一的。

HTML 5 specification says the same thing but in other words. It says that ID must be unique in its home subtree, which is basically the document if we read the definition of it.

HTML 5规范说同样的事情,换句话说。它说ID必须在其主子树中是唯一的,如果我们读取它的定义,它基本上就是文档。

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

You should contact with website developer I guess. Also you can trigger it like this:

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

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

Edit:

That website is using jQuery, so you can use jquery functions, please try again like this:

该网站正在使用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)

#1


0  

HTML 4.01 specification says ID must be document-wide unique.

HTML 4.01规范说ID必须是文档范围内唯一的。

HTML 5 specification says the same thing but in other words. It says that ID must be unique in its home subtree, which is basically the document if we read the definition of it.

HTML 5规范说同样的事情,换句话说。它说ID必须在其主子树中是唯一的,如果我们读取它的定义,它基本上就是文档。

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

You should contact with website developer I guess. Also you can trigger it like this:

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

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

Edit:

That website is using jQuery, so you can use jquery functions, please try again like this:

该网站正在使用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)