I'm trying to scrape data from a website, and the data I want is in a span tag. So far, what I've done is:
var str: Any?
webView.evaluateJavaScript("innerDoc.getElementById(spanId).innerHTML", completionHandler:
{ (html: Any?, error: Error?) in
str = html
})
print(str)
But when I do this I just get nil. If I put print(html)
in place of str = html
I get the output I want.
What am I doing wrong here? Why can't I store that value in a variable?
Evaluating JavaScript takes the WKWebView some time. In this case the
print(str)
statement most likely takes place before the JS script is evaluated. Try moving it inside the completion handler of your evaluation statement and see if it works.