2. Log out (ctrl+alt+del)
> > > import pyautogui
|»_space;> w, h = pyautogui.size()
> > > pyautogui.position()
2. pyautogui.drag(x, y)
a.
»> pyautogui.write(‘Hello, world!’)
b.
»> pyautogui.click(100, 200); pyautogui.write(‘Hello, world!’)
Single characters can use pyautogui.write() (e.g. ‘a’, ‘e’)
Other letters, (e.g. ‘Shift’, ‘F1’) can use:
press() is a ‘wrapper’ of keyDown and Up, which simulate pressing a key and then releasing it.
That is handy, e.g. when you want to keep ‘shift’ pressed until you pressed ‘c’, and only release it afterwards.
Example prompts, XY = 'enter' 'f1' 'esc' 'volumemute'
> > > im = pyautogui.screenshot()
|»_space;> im.save(‘screenshot.png’)
> > > pyautogui.sleep(2)
If it is always the same browser which is in full-screen mode, on the same screen resolution etc., then PyAutoGui is likely to be faster (and yet more prone to error).
Blind to what it is clicking / typing
> > > notepadWindows= getWindowsWithTitle(‘Notepad’)
for win in notepadWindows:
print(win.size)
Specific:
»> ff = pyautogui.getWindowsWithTitle(‘Firefox’)
»> ff[0].size
-> Careful: getWindowS returns a list. To access its content (even if only one match was found), .size or other functions cannot be used on ff.size, but on ff[i].size
in general, 4x functions:
pyautogui.getAllWindows() Returns a list of Window objects for every visible window on the screen.
pyautogui. getWindowsAt(x, y) Returns a list of Window objects for every visible window that includes the point (x, y).
pyautogui. getWindowsWithTitle(title) Returns a list of Window objects for every visible window that includes the string title in its title bar.
pyautogui. getActiveWindow() Returns the Window object for the window that is currently receiving keyboard focus.
> > > pyautogui.mouseInfo()
Disable “3 seconds…”
then move your mouse to the desired pixel coordiantes and press f6
moveTo(x, y) Moves the mouse cursor to the given x and y coordinates.
move(xOffset, yOffset) Moves the mouse cursor relative to its current position.
dragTo(x, y) Moves the mouse cursor while the left button is held down.
drag(xOffset, yOffset) Moves the mouse cursor relative to its current position while the left button is held down.
click(x, y, button) Simulates a click (left button by default).
rightClick() Simulates a right-button click.
middleClick() Simulates a middle-button click.
doubleClick() Simulates a double left-button click.
mouseDown(x, y, button) Simulates pressing down the given button at the position x, y.
mouseUp(x, y, button) Simulates releasing the given button at the position x, y.
scroll(units) Simulates the scroll wheel. A positive argument scrolls up; a negative argument scrolls down.
write(message) Types the characters in the given message string.
write([key1, key2, key3]) Types the given keyboard key strings.
press(key) Presses the given keyboard key string.
keyDown(key) Simulates pressing down the given keyboard key.
keyUp(key) Simulates releasing the given keyboard key.
hotkey([key1, key2, key3]) Simulates pressing the given keyboard key strings down in order and then releasing them in reverse order.
screenshot() Returns a screenshot as an Image object. (See Chapter 19 for information on Image objects.)
getActiveWindow(), getAllWindows(), getWindowsAt(), and getWindowsWithTitle() These functions return Window objects that can resize and reposition application windows on the desktop.
getAllTitles() Returns a list of strings of the title bar text of every window on the desktop.