0

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 Answer 1

1

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

buttonTags[" & buttonloc & "].click();" in current tab
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, well done for figuring it out. Safari needs to know where to direct its JavaScript commands. You had it originally directing it at Window 1. Rather than a tell window 1 block, you could have had a tell document 1 block, and then omitted the ...in current tab. Or, simply, tell app "Safari" to do JavaScript "..." in front document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.