【问题标题】:Android - Check if element exists inside WebView (in the DOM)Android - 检查元素是否存在于 WebView 中(在 DOM 中)
【发布时间】:2015-10-02 14:58:30
【问题描述】:

是否有一种简单干净的方法来检查特定元素(给定其 id)是否存在于已加载到 WebView 中的网页上。

我知道在 Javascript 中我们可以使用document.body.contains(element); 来检查元素是否存在。

如何在我的 Android 应用流程中使用它来查找元素是否存在(在根据元素的存在执行一些其他语句之前)?

【问题讨论】:

  • AFAIK,没有简单的方法可以做到这一点。您可以通过获取 webview 的 html 内容并执行 String.contains 来替代。

标签: javascript java android webview android-webview


【解决方案1】:

你可以放在PageFinish或者webview的其他方法上:

webview.evalueateJavascript("document.getElementById('your id')", object : ValueCallback<String>() {
    override fun onReceiveValue(value: String) {
      Toast.makeText(mContext, "element exist", Toast.LENGTH_SHORT).show()

    }
});

【讨论】:

    【解决方案2】:

    如您所见there,很容易找出是否 webview 包含某个字符串。

    因此,如果您的 webview 仅包含唯一字符串,则有可能 用以下方法检查它:

    String string = "text you want to find";
    webview.findAll(string); //works for API levels 3 - 16
    webview.findAllAsync(string); //works for API levels 16 and up
    

    但这是非常恶意的,因为通常不可能使所有 UI 中的字符串是唯一的。

    【讨论】:

    • 不完全是我想要的。但是我今天学到了一个新东西,所以谢谢。
    猜你喜欢
    • 2011-03-25
    • 2012-09-28
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    相关资源
    最近更新 更多