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 &quot;document.getElementsByClassName('magnifier')[0].click();&quot;
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 &quot;&quot;
    set searchString to searchString as text
    
    tell application &quot;Google Chrome&quot;
        tell front window
            set curTabIndex to active tab index
            set URL of (make new tab) to ¬
                &quot;https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&amp;word=&quot; &amp; ¬
                searchString
            set active tab index to curTabIndex
            
            delay 2.0
            
            do JavaScript &quot;document.getElementsByClassName('magnifier')[0].click();&quot;
        end tell
    end tell
    
end run
do JavaScript is for Safari. execute javascript is for Google Chrome, e.g.:
tell application &quot;Google Chrome&quot; to tell active tab of front window to ¬
    execute javascript &quot;document.getElementsByClassName('magnifier')[0].click();&quot;

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 {&quot;桿&quot;}

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 &quot;&quot;
set searchString to searchString as text

tell application &quot;Google Chrome&quot;
    tell front window
        set curTabIndex to active tab index
        set URL of (make new tab) to ¬
            &quot;https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&amp;word=&quot; &amp; ¬
            searchString
        repeat until (loading of active tab is false)
            delay 1
        end repeat
        tell active tab to execute javascript ¬
            &quot;document.getElementsByClassName('magnifier')[0].click();&quot;
        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 {&quot;桿&quot;}

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 &quot;&quot;
set searchString to searchString as text

tell application &quot;Google Chrome&quot;
    tell front window
        set curTabIndex to active tab index
        set URL of (make new tab) to ¬
            &quot;https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&amp;word=&quot; &amp; ¬
            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 ¬
            &quot;document.getElementsByClassName('magnifier')[0].click();&quot;
    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.