Here's an example. Suppose I want to click the "Show Web Inspector" menu item under the "Develop" menu in Safari. I will first get all of the UIElements in the Develop menu, then iterate over them looking for the UIElement with the proper name. Once found I can click it.
Note that the secret to this is getting the "entire contents" of some UIElement, in this case the Develop menu. That gives me a list of every UIElement in the menu so that I can iterate through them and find whatever I want.
Also note that I have a try block around the if statement. That's because some UIElements don't have a name and it errors, so this just ignores those errors.
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
set developMenu to menu bar item "Develop" of menu bar 1
set allUIElements to entire contents of developMenu
repeat with anElement in allUIElements
try
if name of anElement is "Show Web Inspector" then
click anElement
exit repeat
end if
end try
end repeat
end tell
end tell