Without seeing the complete code, I canβt say exactly what is happening. But judging by the name of your function - getInputByClass2 - I assume that you are trying to get the value of the HTML <input> fields. If so, you should use outPut.push(arr[i].value) instead of outPut.push(arr[i].innerHTML)
As for the second bit of code, your JavaScript has no error handling if the value of document.getElementsByClassName(' field type-string field-Dinfo ')[0] is null.
var els = document.getElementsByClassName(' field type-string field-Dinfo '); //set to value of [0].innerHTML if [0] exists, else empty string var html = els.length ? els[0].innerHTML : ""; //return value to AppleScript html;
update (answer to the updated question)
Running the following script in the script Editor on this StackOverflow page will return the correct value (provided that you have the correct window / tab numbers set). If the search box at the top of this StackOverflow page is empty, you will get an empty string. If you enter the term (but do not submit), then run AppleScript, you will get the value of the field.
tell application "Safari" set DinfoGrab to do JavaScript " document.getElementsByClassName('js-search-field')[0].value;" in tab 1 of window 1 end tell
The only changes from your script are the window / tab numbers, the class name (changed according to the StackOverflow page), and I used value instead of innerHTML .
I tested in the latest version of Safari (10.0.3); if this does not work in your version of Safari, make sure you point to the correct class name. If this DOES script works for you, then the problem is probably related to something on the page you are trying to search, it may be due to the type of <input> field you are retrieving, or to the wrong class name. Itβs possible that updating Safari causes the page to render differently, which indirectly affects your code.
source share