I am working with WKWebView and loaded url in the same web-view which is having login page (email and password input text).
email input text field like this:
<input class="input" type="email" placeholder="" data-automation-id="email" name="email" maxlength="256" value="">
Can someone guide me that how to get the value this input text?
webView.evaluateJavaScript("document.getElementById('email').value", completionHandler: { (jsonRaw: Any?, error: Error?) in
guard let jsonString = jsonRaw as? String else { return }
print(js)
})
Tried this but not working 🙁
TIA
2
Answers
The
input
tag doesn’t have anid
ofemail
which is why the lookup by id is failing. Update the web page so theinput
tag has anid
set toemail
.With the
id="email"
added, thedocument.getElementById('email').value
should find theinput
and give you the value.If you don’t want to add
id
to the element, usegetElementsByClassName
instead which returns an array and pick the first element to read the value of the email field:Example:
Sample HTML string used for testing: