Applescript getInputByClass2 with Safari 10.1

My Applescripts, which I used every day to get text from Safari, have not been working since the last system update

It worked only in Safari, not Safari Preview, I assume that the system for Safari Preview now displayed safari /

tell application "Safari" set DinfoGrab to do JavaScript " document.getElementsByClassName(' field type-string field-Dinfo ')[0].innerHTML;" in tab 3 of window 1 end tell 

with this error:

Safari received an error message: Cant make "document.getElementsByClassName ('field type-string field-Dinfo') [0] .innerHTML;" into text type.

how can i fix this? thanks.

UPDATE:

Here's what works great with Chrome:

 tell application "Google Chrome" tell tab 3 of window 1 to set r to execute javascript "document.getElementsByClassName('field type-string field-Dinfo')[0].innerHTML;" end tell 
+5
source share
1 answer

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.

0
source

Source: https://habr.com/ru/post/1264097/


All Articles