[PYTHON] Using PyAutoGui, I tried to automate the UI test of a VB6 program running on Windows Server 2003 R2 (input command cheat sheet).

Key and mouse operation with winautogui

In the UI test automation that I was doing my best in UWSC, there was an element that did not accept key input by any means (Ore GrapeCity), so the command that I would use when writing an automation script with pyAutoGUI is [PyAuto Gui's documentation](https:: I extracted it from //pyautogui.readthedocs.io/en/latest/index.html).

By the way, the environment I'm doing my best is as follows OS:Windows Server 2003 R2 App development language: Visual Basic 6 Python version used for automation this time: 2.7.18

The site that I referred to in writing this article

Thanks to the great ancestors! …… Visit the Great Predecessors page for how to install modules with pip

Mouse edition

Mouse movement

pyautogui.moveTo(100, 200)  #Coordinate(100,200)Mouse cursor moves to
pyautogui.moveTo(None,500)  #Coordinate(100,500)Mouse cursor moves to
pyautogui.moveTo(600,None)  #Coordinate(600,500)Mouse cursor moves to

Mouse click

pyautogui.click()              #Fire a click event where the mouse cursor is
pyautogui.click(x=100, y=200)  #Mouse cursor coordinates(100,200)Go to and click event fire
pyautogui.click(button='right')#Right click with this

Mouse click application

pyautogui.click(clicks=2)                 #Double click with left click button
pyautogui.click(clicks=2, interval=0.25)  #Left click button click → 0.Wait 25 seconds → left click button click
pyautogui.click(button='right', clicks=3, interval=0.25) #Right mouse button triple click, interval 0.25 seconds

Keyboard input

Send string

pyautogui.write('Hello World!')  # ただ単に「Hello World!」が出力される
pyautogui.write('hello world!', interval=0.25) #0 character by character.Every 25 seconds"hello world!"Is output

Press (press and release) key

pyautogui.press('enter')  #Enter key is pressed
pyautogui.press('f1')     #Function 1[F1]The key is pressed
pyautogui.press('left')   #Left arrow[←]The key is pressed

Key down (hold down the key)

pyautogui.keyDown('shift') # [Shift]Hold down the key. As it is until keyUp
pyautogui.press('left')    #Because it's a press[←]Key pressed and released
pyautogui.press('left')    #Because it's a press[←]Key pressed and released
pyautogui.press('left')    #Because it's a press[←]Key pressed and released
pyautogui.keyUp('shift')   # [Shift]Key goes up(Release the key)

Key operation a little advanced

Give the array element a key code and use it like the write () function

pyautogui.press(['left', 'left', 'left'])        #Realized version with array elements
pyautogui.press('left',presses=3)                #If you hit the same button repeatedly, you can specify the number of times with the presses option
pyautogui.press('left',presses=3, interval=0.25) # write()You can adjust the input interval with the interval option as well as

Simultaneous key press (hot key) function

pyautogui.hotkey('ctrl', 'shift', 'esc') #[Ctrl]+[Shift]+[Esc]Start task manager with

Of course you can write it like this ...

pyautogui.keyDown('ctrl')
pyautogui.keyDonw('shift')
pyautogui.keyDown('esc')
pyautogui.keyUp('esc')
pyautogui.keyUp('shift')
pyautogui.keyup('ctrl')

It's easier to use the hotkey function.

Virtual key list

Here is a list of virtual keys that can be used with pyAutoGui.

['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(',
')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`',
'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace',
'browserback', 'browserfavorites', 'browserforward', 'browserhome',
'browserrefresh', 'browsersearch', 'browserstop', 'capslock', 'clear',
'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal', 'del', 'delete',
'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10',
'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20',
'f21', 'f22', 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
'final', 'fn', 'hanguel', 'hangul', 'hanja', 'help', 'home', 'insert', 'junja',
'kana', 'kanji', 'launchapp1', 'launchapp2', 'launchmail',
'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',
'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6',
'num7', 'num8', 'num9', 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn',
'pgup', 'playpause', 'prevtrack', 'print', 'printscreen', 'prntscrn',
'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select', 'separator',
'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab',
'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen',
'command', 'option', 'optionleft', 'optionright']

It ’s a place that seems to be used unexpectedly

['kana', 'kanji','ctrlright','decimal','numlock']

Or something?

Problems with Japanese strings

This is a copy. Yes. Please sing. Japanese is copy. I did my best. Me too. Is Unicode wrong? Or does it have to be Shift-JIS? When The answer was given by the ancestor.

import pyperclip as clipboard
clipboard.copy("Japanese string")
pyautogui.hotkey('ctrl','v')

This is it. later

unicodedecodeerror 'ascii' codec can't decode

If you unfortunately meet, let's borrow the wisdom of our predecessors and change the default character code to UTF-8.

Summary

Basically, I just summarized the example of Welcome to PyAutoGUI's documentation! + My own trial and It's a record of the error, but I hope there are people who can help with this alone. Let's have a good automation life!

Recommended Posts

Using PyAutoGui, I tried to automate the UI test of a VB6 program running on Windows Server 2003 R2 (input command cheat sheet).
[Python] I tried to make a simple program that works on the command line using argparse.
I tried to automate the construction of a hands-on environment using IBM Cloud's SoftLayer API
I tried to verify the result of A / B test by chi-square test
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I want to take a screenshot of the site on Docker using any font
I want to automate ssh using the expect command!
I tried to compare the accuracy of machine learning models using kaggle as a theme.
I want to automate ssh using the expect command! part2
I tried running PIFuHD on Windows for the time being
I tried to automate [a certain task] using Raspberry Pi
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to output the access log to the server using Node.js
I tried to build a SATA software RAID configuration that boots the OS on Ubuntu Server
I tried to get a database of horse racing using Pandas
I tried to get the index of the list using the enumerate function
I tried to automate the watering of the planter with Raspberry Pi
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to digitize the stamp stamped on paper using OpenCV
I tried to get a list of AMI Names using Boto3
I tried to register a station on the IoT platform "Rimotte"
A command to easily check the speed of the network on the console
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to get the batting results of Hachinai using image processing
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
Verification of how to periodically execute a script on a Linux server on Windows
I made a program to check the size of a file in Python
I tried to perform a cluster analysis of customers using purchasing data
I tried to display the altitude value of DTM in a graph
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to understand the learning function of neural networks carefully without using a machine learning library (first half).
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried the asynchronous server of Django 3.0
I tried to summarize the umask command
I tried to predict the deterioration of the lithium ion battery using the Qore SDK
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
[Python] I tried to judge the member image of the idol group using Keras
I made an appdo command to execute a command in the context of the app
I tried to automate the 100 yen deposit of Rakuten horse racing (python / selenium)
SSH login to the target server from Windows with a click of a shortcut
I tried to make a translation BOT that works on Discord using googletrans
I tried to rescue the data of the laptop by booting it on Ubuntu
I made a program to look up words on the window (previous development)
I made a script to record the active window using win32gui of Python
I tried to automate the face hiding work of the coordination image for wear
I tried to display the infection condition of coronavirus on the heat map of seaborn
I tried to create a model with the sample of Amazon SageMaker Autopilot