I've created a simple projet, the purpose is to load a javascript webpage (https://www.myidtravel.com) and to programatically click a button.
Basically I'm able to do all this stuff but not the click, the wkwebview is unchanged after the call of evaluateJavaScript (the completion Handler is ok with no error)
With Safari or Chrome and the developer tools the click is working correctly: document.getElementById('button-1029').click();
I'm pretty new with JavaScript and I'm curious to see if it's a bug, or a bad implementation...
Bellow you will see my implementation:
import UIKit
import WebKit
class ViewController: UIViewController , WKNavigationDelegate{
var webView : WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.myidtravel.com/")!
webView.load(URLRequest(url: url))
webView.customUserAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK:- WKNavigationDelegate
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("finish to load")
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
webView.evaluateJavaScript("document.getElementById('button-1029').click();", completionHandler: { (value, err) in
print(">>>>>>>>>>>>>>>>>>>> click click")
if let error = err {
print("Error click")
print(error.localizedDescription)
}
})
}
}
}