title: Is there a way to getElementById from a webView in android studio?
body: <p>I'm trying to get the value of my h1 when id="mode" but it only returns <code>I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'innerHTML' of null", source: about:blank (1)</code>. When i try to load the html String directly in the webView it works fine.
  Maybe you know the right way to do this.</p>
  
  <pre><code>fun getMode(ip: String): String {
      var mode = ""
  
      wvDevice[0].settings.javaScriptEnabled = true
      wvDevice[0].settings.domStorageEnabled = true
      wvDevice[0].settings.databaseEnabled = true
  
  
  
      wvDevice[0].webViewClient = object : WebViewClient() {
          override fun onPageFinished(view: WebView, url: String) {
              view.evaluateJavascript("document.getElementById('mode').innerHTML")
              { value -&gt;
                  val splitted = value.split("\"")
                  for (i in splitted.indices){
                      if (splitted[i] == "RGB" || splitted[i] == "TW"){
                          mode = splitted[i]
                          println("mode: $mode")
                      }
                  }
              }
  
          }
      }
  
      /*wvDevice[0].loadData("&lt;html&gt;&lt;head&gt;&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n" +
              "&lt;link rel=\"icon\" href=\"data:,\"&gt;\n" +
              "&lt;link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\"&gt;\n" +
              "&lt;script src=\"https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js\"&gt;&lt;/script&gt;\n" +
              "&lt;/head&gt;&lt;body&gt;&lt;div class=\"container\"&gt;&lt;div class=\"row\"&gt;&lt;h1 id=\"mode\"&gt;TW&lt;/h1&gt;&lt;h2&gt;533&lt;/h2&gt;&lt;/div&gt;\n" +
              "&lt;a class=\"btn btn-primary btn-lg\" href=\"%23\" id=\"change_color\" role=\"button\"&gt;Change Color&lt;/a&gt; \n" +
              "&lt;input class=\"jscolor {onFineChange:'update(this)'}\" id=\"rgb\" autocomplete=\"off\" style=\"background-image: none; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);\"&gt;&lt;/div&gt;\n" +
              "&lt;script&gt;function update(picker) {document.getElementById('rgb').innerHTML = Math.round(picker.rgb[0]) + ', ' +  Math.round(picker.rgb[1]) + ', ' + Math.round(picker.rgb[2]);\n" +
              "document.getElementById(\"change_color\").href=\"?r\" + Math.round(picker.rgb[0]) + \"g\" +  Math.round(picker.rgb[1]) + \"b\" + Math.round(picker.rgb[2]) + \"&amp;\";}&lt;/script&gt;\n" +
              "\n" +
              "&lt;/body&gt;&lt;/html&gt;", "text/html; charset=utf-8", "UTF-8")*/
      wvDevice[0].loadUrl(ip)
  
  
      return mode
  
  }
  </code></pre>