Safari Javascript error- TypeError: null is not an object

Applescript errors when executing a javascript in Safari.



When executed in Safari’s javascript console, the javascript executes successfully and returns an integer as text, reflecting the count of pending rx.

When compiled in Applescript, the javascript looks like this.


tell application "Safari"
	set js to "document.getElementById(\"ap1_header_lblPendingRxCount\").innerText"
	set PendingRxCount to do JavaScript js in document 1
end tell

When executed in AppleScript, Safari’s Javascript console returns a “null is not an object” error:

Console error:

TypeError: null is not an object (evaluating ‘document.getElementById(“ap1_header_lblPendingRxCount”).innerText’)

How can I correct this Applescript to have it successfully execute this javascript?

I’m not a JS expert but as far as I can see, your original JS script differs from what you send in AppleScript:

you have another document.getElementById in the original script

Thank you leo_r, and I have since corrected the javascript.