How to click a button using AppleScript

applescript

I'm trying to press a button in TeamSpeak using applescript, here is my code:

tell application "TeamSpeak 3 Client" to activate
tell application "System Events"
    tell process "TeamSpeak 3"
        click menu item "Connect" of menu "Connections" of menu bar 1
        delay 2
        click button "Connect"
    end tell
end tell

and here is an image of me hovering over the button in accessibility inspector

Accessibility Inspector inspecting a push button

All I want to be able to do is figure out how to push my button using the information given to me in Accessibility Inspector, but with my code I get the error "System Events got an error: Can’t get button "Connect" of process "TeamSpeak 3"." Here is a screenshot of the window the button is directly inside:

enter image description here

Best Answer

You also need to specify the parent objects.

activate application "TeamSpeak 3 Client"
tell application "System Events" to tell process "TeamSpeak 3"
    click menu item "Connect" of menu "Connections" of menu bar 1
    click button "Connect" of window 1 of window 1
end tell