【问题标题】:Automate Webview with INJECT_EVENTS permission使用 INJECT_EVENTS 权限自动化 Webview
【发布时间】:2023-02-04 17:40:48
【问题描述】:

有这个网站我需要用用户登录并每次都通过。我一直在使用下面的代码将代码注入网站的文本框。 (登录后还有更多文本框需要自动填写)

wv.loadUrl("javascript:document.getElementById('SSUser_Logon_0-item-USERNAME').focus();");
wv.evaluateJavascript("try{document.activeElement.value='"+User+"';}catch(e){}",null);

我现在收到错误

Injecting input events requires the caller (or the source of the instrumentation, if any) to have the INJECT_EVENTS permission.

  <uses-permission android:name="android.permission.INJECT_EVENTS" />

抛出这个permission is only granted to system apps 的错误。

我需要帮助来注入代码,或者找到另一种替代方法来自动填写网站上的文本框。

【问题讨论】:

    标签: android webview


    【解决方案1】:

    要解决 INJECT_EVENTS 权限错误,一种替代解决方案是使用 WebViewClientshouldInterceptRequest 方法在加载页面之前以编程方式将所需的输入值注入页面的 DOM。这样,您就不必依赖于将事件注入 WebView,这需要 INJECT_EVENTS 权限。

    这是一个基本示例,说明如何使用 shouldInterceptRequest 注入用户并传递到文本框:

    WebViewClient webViewClient = new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            String url = request.getUrl().toString();
            if (url.equals("https://your-target-login-page.com")) {
                String injection = "javascript:(function() { " +
                                   "document.getElementById('SSUser_Logon_0-item-USERNAME').value = '" + User + "';" +
                                   "document.getElementById('SSUser_Logon_0-item-PASSWORD').value = '" + Pass + "';" +
                                   "})()";
                view.loadUrl(injection);
            }
            return super.shouldInterceptRequest(view, request);
        }
    };
    wv.setWebViewClient(webViewClient);
    

    这应该允许您以编程方式填写输入字段,而无需 INJECT_EVENTS 许可。

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 2012-04-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多