My goal:
- Run jquery script to click "add to cart" button from a 'specific' third party website product page.
Expected Result:
Running the script via evaluateJavaScript from WKWebView should allow me to click on the checkout button and add the product to cart.
Actual Result:
Running my script via evaluateJavascript gives me a javascript exception(provided below):
Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=0, WKJavaScriptExceptionMessage=, WKJavaScriptExceptionColumnNumber=0, NSLocalizedDescription=A JavaScript exception occurred}
Unlike similar questions related to getting a javascript exception from evaluateJavaScript, my exception was returning a much rather broad exception. "WKJavaScriptExceptionMessage" is usually very descriptive but there was nothing of value that I can take from it to fix my javascript issue.
When testing, script runs fine on the console of google chrome and safari inspector(for mobile). It was able to perform the expected results with no issue. So I'm not sure why I'm getting an exception in the first place when running the script from evaluateJavascript.
If you are curious this is my code from initializing/viewDidLoad:
let config = WKWebViewConfiguration()
let contentController = WKUserContentController()
config.processPool = CurrentProcessPool.shared.getCurrentProcessPool()
config.websiteDataStore = WKWebsiteDataStore.default()
config.userContentController = contentController
let webViewModel = WKWebView(frame: window.bounds, configuration: config)
let productPageLink = "https://click.linksynergy.com/link?id=7yE5K5lr0eU&offerid=639733.3977&type=15&murl=https%3A%2F%2Fwww.rebeccaminkoff.com%2F%2F%2Fproducts%2Fsandra-puffer-f19100911-khaki"
let url = myURL(string: productPageLink)
DispatchQueue.main.async {
webViewModel.load(URLRequest(url: url))
}
This is where I load the script:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
let size = "XXS"
let myScript = """
document.querySelector('.size-select [data-size-option = "\(size)" i]').click()
document.querySelector('.add-to-cart').click()
"""
webView.evaluateJavaScript(myScript, completionHandler: { (result, error) in
print(result)
print(error)
})
}
Let me know if there are areas that need clarification and I would happily fill in that information.
Any insights to why the javascript exception is not providing more detailed information would be great! Thanks!
from Javascript exception when calling evaluateJavaScript
No comments:
Post a Comment