Javascript within AppleScript 'missing value&#

2019-08-27 12:10发布

问题:

I have the following AppleScript with Javascript contained:

set buttontext to "Add Option"
set buttonloc to 1

tell application "Safari"
    activate
    tell window 1
        do JavaScript "var buttonTags = document.getElementsByTagName(\"button\"); 
        var searchText = \"" & buttontext & "\"; 
        var found; 
        for (var i = 0; i < buttonTags.length; i++) 
        {if (buttonTags[i].textContent == searchText) {
        found = buttonTags[i]; break;
        }
        }
        buttonTags[" & buttonloc & "].click();"
    end tell
end tell

It compiles fine but upon execution there is no action and I receive a 'missing value' error. It is designed to traverse a web page in Safari and allow a user to specify the button text and rank to be able to click it.

When execute this Javascript directly in Safari's dev console it works, but I need to have it in AppleScript to wrap it into a longer routine.

Advice appreciated!

回答1:

I have managed to solve this with amending the end of the JS to:

buttonTags[" & buttonloc & "].click();" in current tab