Interacting with X applications programmatically
Give xdotool (Ubuntu man page) a look.
It's extremely powerful and should be able to do whatever you need.
http://www.semicomplete.com/projects/xdotool/xdotool.xhtml
xte
, part of xautomation is another tool that you can use to generates fake X11 input using the XTest extension.
From the homepage:
Control X from the command line for scripts, and do "visual scraping" to find things on the screen. The control interface allows mouse movement, clicking, button up/down, key up/down, etc, and uses the XTest extension so you don't have the annoying problems that xse has when apps ignore sent events. The visgrep program find images inside of images and reports the coordinates, allowing progams to find buttons, etc, on the screen to click on.