import WebKit
を記述WKNavigationDelegate
を継承する- viewDidLoadに以下を追加
インスタンス.navigationDelegate = self
インスタンス.load(URLRequest(url: URL(string: "https://xxx.xx)!))
- webViewメソッドを定義
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { mainWebView.evaluateJavaScript("document.getElementById('loginId').value = 'maintenance';") mainWebView.evaluateJavaScript("document.getElementById('submitButton').click();") }
import UIKit
import WebKit
class SecondViewController: UIViewController,WKNavigationDelegate {
@IBOutlet weak var mainWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
mainWebView.navigationDelegate = self
mainWebView.load(URLRequest(url: URL(string: "https://google.com/")!))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
mainWebView.evaluateJavaScript("document.querySelector('[name=\"q\"]').value = 'hoge';")
mainWebView.evaluateJavaScript("document.getElementById('tsf').submit();")
}
}
コメント