I’m trying to simulate a click at a specific point on a WKWebView, and found evaluateJavaScript’s elementAtPoint suited what I needed. However it wouldn’t operate correctly in my project — doing nothing besides printing error messages such as nil is not an element, so I tried to run it in a test project on various websites, where it also failed.
I’m using the following code (where the user activates the run action with a button):
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
@IBAction func run(_ sender: Any) {
DispatchQueue.main.async { [self] in
webView.evaluateJavaScript("document.elementFromPoint(219, 23).click();", completionHandler: { [self] (element, error) in
if error != nil {
print(error)
} else {
}
})
}
}
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.scrollView.contentInsetAdjustmentBehavior = .never
webView.load(URLRequest(url: URL(string: "https://theage.com.au")!))
}
}
Can anyone spot the issue here? Am I missing a property change or something?
I’ve also linked the test project here (https://github.com/TheIntelCorei9/DOMTest) so you can see the issue in a bit more depth.
Thanks!