Safari - AppleScript does not move to the next section of a bookdown online book I am writing a script to print a section of a bookdown online book as PDF, then move to the next section, and so on.
The print part works (the key codes are from this page):
tell application &quot;Safari&quot;
    
    activate
    
    tell application &quot;System Events&quot;
        key code 35 using command down -- activate print menu item
    end tell
    
    delay 0.5
    
    set i to 0
    repeat while i &lt; 15
        set i to i + 1
        delay 0.1
        tell application &quot;System Events&quot;
            key code 48 -- press tab 15 times
        end tell
    end repeat
    
    tell application &quot;System Events&quot;
        key code 49 -- press space
    end tell
    
    set i to 0
    repeat while i &lt; 2
        set i to i + 1
        delay 0.1
        tell application &quot;System Events&quot;
            key code 125 -- press down key twice
        end tell
    end repeat
    
    tell application &quot;System Events&quot;
        key code 36 -- enter
    end tell
    
    set i to 0
    repeat while i &lt; 16
        set i to i + 1
        delay 0.1
        tell application &quot;System Events&quot;
            key code 125 -- press tab to get to &quot;save&quot;
        end tell
    end repeat
    
    tell application &quot;System Events&quot;
        key code 36 -- enter to cleck on save
    end tell
    
end tell

Problem
Now that I have printed the current section and I am back on Safari, I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
I have tried to add the following to the script above:
tell application &quot;System Events&quot;
        key code 124 -- right arrow to enter the next page
    end tell

Or even to &quot;reopen&quot; Safari, but nothing happens.
tell application &quot;Safari&quot;
    
    activate
    
    tell application &quot;System Events&quot;
        key code 124 -- right arrow to move to the next section
    end tell
    
end tell

How can I have AppleScript &quot;turn the page&quot; and move to the next section?
Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating &quot;tab&quot; 15 times. I have looked at the Accessibility Inspector and found that &quot;PDF&quot; in the print menu corresponds to NSPopUpButtonCell. I have tried to use select NSPopUpButtonCell of its sheet but it did not work.
I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
How can I have AppleScript &quot;turn the page&quot; and move to the next section?

If you are trying to programmatically click the right-arrow, as shown in the image below, then the following example AppleScript code can do that:
tell application &quot;Safari&quot; to ¬
    tell document 1 to ¬
        do JavaScript ¬
            &quot;document.getElementsByClassName('fa fa-angle-right')[0].click();&quot;


Notes:
This requires Allow JavaScript from Apple Events to be check on the hidden Develop menu.
To unhide the hidden Develop menu:

Safari &gt; Preferences… &gt; Advanced &gt; [√] Show Develop menu in menu bar




Update to address:

Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating &quot;tab&quot; 15 times.

Here is how I'd use the JavaScript from above and coded to avoid using the key code and or keystroke System Events  commands, especially tabbing around the UI.
Example AppleScript code:
tell application &quot;System Events&quot;
    
    tell application process &quot;Safari&quot;
        
        set frontmost to true
        
        set i to 0
        repeat until (its frontmost = true)
            delay 0.1
            set i to i + 1
            if i ≥ 20 then return
        end repeat
        
        click menu item &quot;Print…&quot; of ¬
            menu 1 of ¬
            menu bar item &quot;File&quot; of ¬
            menu bar 1
        
        tell its front window
            
            set i to 0
            repeat until (exists menu button &quot;PDF&quot; of sheet 1)
                delay 0.1
                set i to i + 1
                if i ≥ 20 then return
            end repeat
            
            click menu button &quot;PDF&quot; of sheet 1
            
            set i to 0
            repeat until (exists menu item &quot;Save as PDF&quot; of ¬
                menu 1 of menu button &quot;PDF&quot; of sheet 1)
                delay 0.1
                set i to i + 1
                if i ≥ 20 then return
            end repeat
            
            click menu item &quot;Save as PDF&quot; of ¬
                menu 1 of ¬
                menu button &quot;PDF&quot; of ¬
                sheet 1
            
            set i to 0
            repeat until (exists button &quot;Save&quot; of sheet 1 of sheet 1)
                delay 0.1
                set i to i + 1
                if i ≥ 20 then return
            end repeat
            
            click button &quot;Save&quot; of sheet 1 of sheet 1
            
            set i to 0
            repeat while (exists sheet 1)
                delay 0.1
                set i to i + 1
                if i ≥ 100 then return
            end repeat
            
        end tell    
    end tell
end tell

tell application &quot;Safari&quot; to ¬
    tell document 1 to ¬
        do JavaScript ¬
            &quot;document.getElementsByClassName('fa fa-angle-right')[0].click();&quot;


Notes:
Since this type of AppleScript script is using UI Scripting, I have included an error handling in the form of a repeat loop to wait up to two seconds for the target UI element to become available to be acted upon for most of the targets, however the last repeat loop waits longer because it has to wait until the Save as PDF to complete. A simple delay command with an appropriate value could be used instead, but with the include delay of a tenth of a second in the repeat loops it shouldn't have to wait any longer than need be. In other words, I'd only use simple delay command if I want to slow the script down from going through the various events of the UI. It's doubtful that it would need to be adjusted, but obviously do so as/if necessary.
If the timeout is reached, the script aborts at that point without any error message. The single-line if i ≥ 20 then return statements can be turned into a full if block and include an error message via the display dialog, display alert, or display notification command as wanted.