pyautogui automation tool use

In order to deal with some trivial things, I tested the function of pyautogui. The realized function is to send specified information to specified WeChat friends. Here
, we did not use the API of WeChat to send directly, but realized it by simulating real operations, mainly to understand the use of some automation tools.
The pyautogui library of python is used here.
The main function is to automatically open the computer version of WeChat, search for specified friends, open a friend dialog box, enter text and send it.

The following is the implementation code

# Import necessary libraries
import pyautogui
import time

# Locate WeChat icon on the taskbar and click it
wechat_icon = pyautogui.locateOnScreen('image/wechat_icon.png', confidence=0.9)

pyautogui.doubleClick(wechat_icon)
time.sleep(3)

# Wait for WeChat to open
# Locate the search bar and click it
search_bar = pyautogui.locateOnScreen('image/search_bar.png', confidence=0.9)
pyautogui.click(search_bar)
time.sleep(2)

# Type the name of the friend to send message to
pyautogui.typewrite("name")
pyautogui.press('enter')
# Wait for search results to load
time.sleep(2)

# Click on the friend's name in the search results
friend_name = pyautogui.locateOnScreen('image/name.png', confidence=0.9)
pyautogui.doubleClick(friend_name)

# Wait for the chat window to open
time.sleep(2)

# Type the message to send
pyautogui.typewrite('Hello, SX!')

# Press Enter to send the message
pyautogui.press('enter')

Most people have introduced the api of pyautogui, so I won't go into details here. Here we mainly explain the points that need to be paid attention to in practice.

1: What is the address of the picture in the api of Pyautogui to find pictures?

wechat_icon = pyautogui.locateOnScreen('image/wechat_icon.png', confidence=0.9)

That is, what is this 'image/wechat_icon.png' and where does it come from?

insert image description here
As shown in the picture above, image/wechat_icon.png is actually the address of the picture. This picture is a screenshot of the desktop. You can capture the corresponding icon with other tools, and then pyautogui will find the picture from your screen.
**The tricky thing here is that the screenshot must be exactly the same as the picture you are looking for, otherwise you will not find it, that is, if you take a screenshot and then move the icon to a position, then I am sorry, there is a high probability that you will not be able to find it. To put it more broadly, you will not be able to find this application even if you change computers in this way. Of course, there is no solution. For example, when you open WeChat, the search box on WeChat is the same. You can also simulate clicking pyautogui.click(100,100) by specifying the location **

2: Pyautogui cannot input Chinese

//这两个方法不支持输入中文
pyautogui.write()
pyautogui.typewrite()

can be replaced by

pyperclip.copy('中国')
pyperclip.paste() 

that's all.

Guess you like

Origin blog.csdn.net/qq_39860954/article/details/129988432