With the mouse cursor over the drawing application’s canvas and the Pencil or Brush tool selected, enter the following into a new file editor window and save it as spiralDraw.py:
.pillow
module before continuing with this section.On Linux computers, the scrot
program needs to be installed to use the screenshot functions in PyAutoGUI. In a Terminal window, run sudo apt-get install scrot
to install this program. If you’re on Windows or OS X, skip this step and continue with the section.
typewrite()
to simulate pressing any combination of keys.You can also examine the pyautogui.KEYBOARD_KEYS
list to see all possible keyboard key strings that PyAutoGUI will accept. The 'shift'
string refers to the left SHIFT key and is equivalent to 'shiftleft'
. The same applies for 'ctrl'
, 'alt'
, and 'win'
strings; they all refer to the left-side key.
pyautogui.press()
function, which calls both of these functions to simulate a complete keypress.Run the following code, which will type a dollar sign character (obtained by holding the SHIFT key and pressing 4):
for information onImage
objects.)Here are the steps for entering data into the form:
Click the Name field. (Use mouseNow.py to determine the coordinates after maximizing the browser window. On OS X, you may need to click twice: once to put the browser in focus and again to click the Name field.)
Type a name and then press TAB.
Type a greatest fear and then press TAB.
Press the down arrow key the correct number of times to select the wizard power source: once for wand, twice for amulet, three times for crystal ball, and four times for money. Then press TAB. (Note that on OS X, you will have to press the down arrow key one more time for each option. For some browsers, you may need to press the ENTER key as well.)
Press the right arrow key to select the answer to the RoboCop question. Press it once for 2, twice for 3, three times for 4, or four times for 5; or just press the spacebar to select 1 (which is highlighted by default). Then press TAB.
Type an additional comment and then press TAB.
Press the ENTER key to “click” the Submit button.
After submitting the form, the browser will take you to a page where you will need to click a link to return to the form page.
Note that if you run this program again later, you may have to update the mouse click coordinates, since the browser window might have changed position. To work around this, always make sure the browser window is maximized before finding the coordinates of the first form field. Also, different browsers on different operating systems might work slightly differently from the steps given here, so check that these keystroke combinations work for your computer before running your program.
nameField
variable in your program. Also, find out the x- and y-coordinates and RGB tuple value of the blue Submit button. These values will be assigned to the submitButton
and submitButtonColor
variables, respectively.Next, fill in some dummy data for the form and click Submit. You need to see what the next page looks like so that you can use mouseNow.py to find the coordinates of the Submit another response link on this new page.
Make your source code look like the following, being sure to replace all the values in italics with the coordinates you determined from your own tests:
. This tutorial explains how to create a GUI automation program in Python that plays a Flash game called Sushi Go Round. The game involves clicking the correct ingredient buttons to fill customers’ sushi orders. The faster you fill orders without mistakes, the more points you get. This is a perfectly suited task for a GUI automation program—and a way to cheat to a high score! The tutorial covers many of the same topics that this chapter covers but also includes descriptions of PyAutoGUI’s basic image recognition features.