I am using self.webView.stringByEvaluatingJavaScript(from: "document.getElementById('username').value = \\"\\(username)\\"")
to put in a username, and it works fine.
A different webpage has this field:
<input type="text" maxlength="255" name="user" class="stdWidth" value="">
and this field:
<input type="password" maxlength="255" password="1" name="pass" class="stdWidth">
How would i use stringByEvaluatingJavaScript
to put text into this field, as it has no ID?
I hace tried "document.getElementByTagName"
but it didnt work.
The webpage I am trying to input to is: Link
Try like this.you have class
field so use document.querySelector()
to select your element.Example below.
var input = document.querySelector(".stdWidth"); input.value = "\\"Hello\\""; //add event listener on edit like this.. input.addEventListener('change',function(){ var value = this.value; console.log(value);//this is current value of your field });
<!-- begin snippet: js hide: false console: true babel: false -->
UPDATE
The getElementsByTagName()
method returns a collection of all elements in the document with the specified tag name, as a NodeList object.
var input = document.getElementsByTagName('input'); input[0].value = "\\"username\\""; input[1].value = "\\"password\\"";
<input type="text" maxlength="255" name="user" class="stdWidth" value=""> <input type="text" maxlength="255" name="pass" class="stdWidth" value="">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.