如何以编程方式在 WebView 中输入表单输入字段,然后单击提交?

发布于 2024-12-01 18:20:54 字数 1685 浏览 4 评论 0原文

我在解析 WebView HTML 或加载 URL(相对或绝对)时没有任何问题,但我对如何以编程方式“模拟”用户输入用户名和地址感到困惑。密码,然后单击提交/登录按钮,给出以下 HTML:(

<form method="post" class="mobile-login-form" id="login_form" action="https://www.example.com/login.php?m=m&amp;refsrc=http%3A%2F%2Fm.example.com%2F&amp;refid=0">
  <input type="hidden" name="lsd" autocomplete="off">
  <input type="hidden" name="post_form_id" value="1b3cf017c90d483cc50fcac3f2a9a283">
  <input type="hidden" name="charset_test" value="€,´,€,´,?,?,?">
  <input type="hidden" name="version" value="1">
  <input type="hidden" id="ajax" name="ajax" value="1">
  <input type="hidden" id="width" name="width" value="313">
  <input type="hidden" id="pxr" name="pxr" value="1.5">
  <input type="hidden" id="gps" name="gps" value="1">
  <div class="bgx msf" data-sigil="intfs">
    <div class="mfss fcg">Username:
    </div>
    <input class="input mobile-login-field" name="username" value="" type="text">
  </div>
  <div class="bgx msf" data-sigil="intfs">
    <div class="mfss fcg">Password:
    </div>
    <input class="input mobile-login-field" autocorrect="off" autocapitalize="off" name="pass" type="password">
  </div>
  <div class="bgx msf" data-sigil="intfs">
    <label class="btn btnC touchable" data-sigil="blocking-touchable">
      <input type="submit" value="Log In" class="mfss" name="login">
    </label>
  </div>
</form>

我不控制该 HTML,这只是任意网站提示的示例)

我不需要执行所谓的“静默身份验证” - 我只是想为用户提供一个方便的快捷方式,单击按钮而不是重新输入用户名和密码。一遍又一遍地输入密码(以防执行清除缓存清除数据)。

I don't have any problem parsing WebView HTML or loading URLs (relative or absolute) but I am stumped as to how to programmatically "simulate" the user entering username & password, then clicking the submit/login button, given the following HTML:

<form method="post" class="mobile-login-form" id="login_form" action="https://www.example.com/login.php?m=m&refsrc=http%3A%2F%2Fm.example.com%2F&refid=0">
  <input type="hidden" name="lsd" autocomplete="off">
  <input type="hidden" name="post_form_id" value="1b3cf017c90d483cc50fcac3f2a9a283">
  <input type="hidden" name="charset_test" value="€,´,€,´,?,?,?">
  <input type="hidden" name="version" value="1">
  <input type="hidden" id="ajax" name="ajax" value="1">
  <input type="hidden" id="width" name="width" value="313">
  <input type="hidden" id="pxr" name="pxr" value="1.5">
  <input type="hidden" id="gps" name="gps" value="1">
  <div class="bgx msf" data-sigil="intfs">
    <div class="mfss fcg">Username:
    </div>
    <input class="input mobile-login-field" name="username" value="" type="text">
  </div>
  <div class="bgx msf" data-sigil="intfs">
    <div class="mfss fcg">Password:
    </div>
    <input class="input mobile-login-field" autocorrect="off" autocapitalize="off" name="pass" type="password">
  </div>
  <div class="bgx msf" data-sigil="intfs">
    <label class="btn btnC touchable" data-sigil="blocking-touchable">
      <input type="submit" value="Log In" class="mfss" name="login">
    </label>
  </div>
</form>

(I don't control that HTML, this is just an example for an arbitrary website prompt)

I don't need to do so called "silent authentication" - I merely want to provide a convenience shortcut for the user clicking a button instead of re-typing username & password over and over again (in case, clear cache or clear data were performed).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-12-08 18:20:54

您是否尝试过使用猴子工具?应该可以完成这个任务。

Have you tried using monkey tools. Should be able to do that task.

相思故 2024-12-08 18:20:54

在 html 页面中定义 JS 函数并使用以下方法调用它。

 private void callJavaScriptFunction(String functionName, List<String> params) {
            String javaScript = "javascript:";

        javaScript += functionName + " ( ";

        if (params != null) {
                for(int i = 0; i < params.size(); i++) {
                        javaScript += params.get(i);

                        if (i != params.size() - 1) {
                                javaScript += ", ";
                        }
                }
        }

        javaScript += ");";

        Log.d(javaScript);

        webView.loadUrl(javaScript);
}

Define JS function within your html page and call it with following method.

 private void callJavaScriptFunction(String functionName, List<String> params) {
            String javaScript = "javascript:";

        javaScript += functionName + " ( ";

        if (params != null) {
                for(int i = 0; i < params.size(); i++) {
                        javaScript += params.get(i);

                        if (i != params.size() - 1) {
                                javaScript += ", ";
                        }
                }
        }

        javaScript += ");";

        Log.d(javaScript);

        webView.loadUrl(javaScript);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文