1

I've a p tag with id test in my webpage, when I put it as follows everything works well.

html,

<p id="test">hhhhhhhhhhhh</p>
<body ng-app="home" ng-controller="HomeController" style="min-height:700px;">
  <printerinfo></printerinfo>
</body>

swift3,

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    self.wk.evaluateJavaScript("document.getElementById('test').innerHTML;") { (result, error) -> Void in
    ->
       if error != nil {
            print(error)
       }
        print(result)
    }
}

While I move the p tag to template printerinfo, I get

UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=TypeError: null is not an object (evaluating 'document.getElementById('test').innerHTML')

most of the cases, and the remain cases works well with result Optional(hhhhhhhh)

And here is the ios wkwebview page content viewed from Safari technology Preview's develpe > iphone device name's window. And I can access the innerHTML from the console. enter image description here

For now I guess that the evaluateJavaScript has executed before the anjular has finished feeding the webpage.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
LF00
  • 27,015
  • 29
  • 156
  • 295

1 Answers1

0

It's caused by webpage's dynamic content. I solved this by useing javascript to check when the page is loaded, then notify native swift code to do next process.

For detail check this post.

Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295