2

I have a UIViewController that has a WKWebView that loads a particular view with a button that needs to be clicked automatically. To tackle this, I used the WKNavigationDelegate protocol function didFinish and an utilizing WKWebView's evaluateJavaScript method. Here is my implementation

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        webView.evaluateJavaScript("document.getElementsByClassName('myButtonClass')[0].click()") { (result, error) in
            print("ERROR: \(error)")
            print("RESULT: \(result)")
        }
    }

So once the page is loaded, I am attempting to click a certain button on the screen (keep in mind, there is only one button with this class name on this particular page view). When this code runs, both result and error get printed out, and both of them are nil. Yet, the button is not clicked and WKWebView never redirects to the link the button links to.

How is it possible that there is no error but nothing happens?

Am I missing some sort of navigation set up or something?

2
  • have you figured this out ? Commented Jan 4, 2020 at 4:17
  • I got it to work using the accepted answer in this question stackoverflow.com/questions/47523277/… Commented Jan 4, 2020 at 4:43

1 Answer 1

0

I think you are missing ; inside the script. Try this

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    webView.evaluateJavaScript("document.getElementsByClassName('myButtonClass')[0].click();") { (result, error) in
        print("ERROR: \(error)")
        print("RESULT: \(result)")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.