Automator script to click button on web page generates “The operation couldn’t be completed." error
The AppleScript below is supposed to click the first element with the magnifier class on a predefined web page.
The script runs successfully if you comment out the JavaScript line, do JavaScript "document.getElementsByClassName('magnifier')[0].click();"
From the Chrome console, this line of JavaScript executes successfully.
But if you include the JavaScript line, the following error occurs:
The action “Run AppleScript” encountered an error: “The operation couldn’t be completed. (com.apple.Automator error -212.)”
Anyone have any ideas what the problem could be?
on run {input, parameters}
set searchString to input as text
set AppleScript's text item delimiters to space
set searchString to text items of searchString
set AppleScript's text item delimiters to ""
set searchString to searchString as text
tell application "Google Chrome"
tell front window
set curTabIndex to active tab index
set URL of (make new tab) to ¬
"https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&word=" & ¬
searchString
set active tab index to curTabIndex
delay 2.0
do JavaScript "document.getElementsByClassName('magnifier')[0].click();"
end tell
end tell
end run
do JavaScript
is for Safari. execute javascript
is for Google Chrome, e.g.:
tell application "Google Chrome" to tell active tab of front window to ¬
execute javascript "document.getElementsByClassName('magnifier')[0].click();"
That said, where you have it placed in the script may not be where you really want it.
Here is some example AppleScript code run in Script Editor with Google Chrome opened with at least one window.
It waits for the target URL to finish loading before clicking the magnifying glass and then sets the active tab
back to the original active tab.
set input to {"桿"}
set searchString to input as text
set AppleScript's text item delimiters to space
set searchString to text items of searchString
set AppleScript's text item delimiters to ""
set searchString to searchString as text
tell application "Google Chrome"
tell front window
set curTabIndex to active tab index
set URL of (make new tab) to ¬
"https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&word=" & ¬
searchString
repeat until (loading of active tab is false)
delay 1
end repeat
tell active tab to execute javascript ¬
"document.getElementsByClassName('magnifier')[0].click();"
delay 0.5
set active tab index to curTabIndex
end tell
end tell
Update to address comment.
The same conditions mentioned above applies to this example AppleScript code:
set input to {"桿"}
set searchString to input as text
set AppleScript's text item delimiters to space
set searchString to text items of searchString
set AppleScript's text item delimiters to ""
set searchString to searchString as text
tell application "Google Chrome"
tell front window
set curTabIndex to active tab index
set URL of (make new tab) to ¬
"https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&word=" & ¬
searchString
set active tab index to curTabIndex
repeat until (loading of last tab is false)
delay 1
end repeat
tell last tab to execute javascript ¬
"document.getElementsByClassName('magnifier')[0].click();"
end tell
end tell
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.