Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use pyautogui in python

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to install and use pyautogui in python. I think it is very practical, so I share it with you. I hope you can get something after reading this article.

The following is my study and arrangement of pyautogui, one of the most important scripting libraries in python, and I hope it will be helpful to share it with you.

Tip: in the course of my initial use of pyautogui, I found that click clicks on Google browser are invalid, and others have not been found yet.

Installation of pyautogui

You can use cmd to enter the installation of the pip install pyautogui progressive library

Mouse

The control of the mouse is based on the position on the screen. For example, the display resolution of my computer is 1920 ✖ 1080, that is, starting from the upper left corner of the screen, the horizontal direction represents x to the right, there are 1920 small pixel blocks in a row, and the vertical direction represents y, and there are 1080 pixel blocks in a column, just like a large coordinate system, except that there is only the first quadrant in the whole screen.

Use size to get the display resolution of the screen

Examples are as follows:

From pyautogui import sizex,y=size () print (xQuery y)

Use position to get the current mouse position

From pyautogui import positionx,y=position () print (xQuery y)

Mouse movement

MoveTo: move the mouse to a location on the screen

Above, we can see that the coordinate of the upper left corner is (0jue 0), and the lower right corner is (1920 moment 1080). The example is as follows

From pyautogui import moveTomoveTo (1684 and 1059) # 1684 and 1059 are the positions to be moved to, and duration indicates the time it takes to perform the move, in seconds

MoveRel (move): move based on the current location

From pyautogui import moveTo,moveRel moveRel # moves from the current mouse position to the position of 100th to the right and 200th to the bottom, and the process time is set to 2 seconds of mouse click

Click: click the mouse

The coordinates are specified in the from pyautogui import click#click, and you can also set the duration time from execution to clicking on this location, as well as the left, middle or right-click click.) click (100meme 200mpcmlmlmlmlmlt) click (100meme 200mpcmlmlc)

DoubleClick: double-click the mouse

The from pyautogui import doubleClick# parameter is the same as the click, except that now it is double-click the mouse doubleClick (100pc200) doubleClick (100pjc200) doubleClick (100pc200)

MouseDown and mouseUp: mouse press and release

From pyautogui import mouseDown,mouseUpmouseDown () # Mouse Down mouseUp () # Mouse release

Mouse drag

DragTo: drag the mouse to a location

DragRel: drag the table according to the current position

Mouse drag is similar to mouse movement, but in comparison, a test of the difference of a mobile window is made.

From pyautogui import moveTo,mouseDown,mouseUp mouseDown (button='left') moveTo (1000500) mouseUp (button='left') from pyautogui import dragTo,mouseDown,mouseUp mouseDown (button='left') dragTo (1000500) mouseUp (button='left')

During the test, it is found that the combination of moveTo and mouse press can drag a window, but dragTo cannot.

Mouse scrolling from pyautogui import scroll scroll (300) # passes in an integer, and a positive number indicates sliding the wheel upward scroll (- 300) # sliding down the wheel screen processing

Screenshot: capture full screen

The screenshot will be saved in the same directory where the script is saved

From pyautogui import screenshotscreenshot () .save ('screenshot .png') # jpg format is also supported, please try other formats

Crop takes screenshots of any size, such as crop (

LocateOnScreen: find the location of screenshot

The location of the screenshot is found on the screen to find the location that matches the saved screenshot.

From pyautogui import locateOnScreenprint (locateOnScreen ('screenshot .png') keyboard input

KeyDown and keyUp: keyboard press and keyboard release

Multiple keys can be pressed at the same time by pressing and releasing the keyboard, such as ctrl+v paste, etc. In order to achieve multiple keys at the same time, we also have hotkey ('ctrl','c'), which can accept multiple parameters, press according to the incoming order, and then release in the opposite order.

From pyautogui import keyDown,keyUpkeyDown ('space') # Press the space bar keyUp (' space') # release the space bar keyDown ('ctrl') keyDown (' c') keyUp ('c') keyUp ('ctrl') hotkey (' ctrl','c')

Press: release immediately after pressing the key

It can be seen as a combination of press and release, for example, if we want to send a message, press enter, and then release it, so press is widely used.

The case of letters does not affect keystrokes, such as Enter, which we can write as press ('enter'), press (' ENter')

Some have two keys, such as Shift, followed by left or right,press ('shiftleft').

It should be emphasized that the enter button is recognized as a line feed and can be replaced by\ n, that is, press ('enter') = press ('\ n'), and the tab key can be replaced by\ t

The next key is up,down,left,right.

Typewrite (): continuous typing

The typewrite here can recognize case.

From pyautogui import typewritetypewrite ('ceshi',0.5) # the first parameter is what you want to enter, and the second parameter is the interval between each keystroke (typewrite) ([' clockwise typewrite]) # you can also pass in a list of individual strings, typewrite (['sonic, grammatical, grammatical,]) # typewrite (' Sun') 0.5) # will enter the uppercase S prompt message box from pyautogui import alertprint (alert (text=' a test', title='test')) # Click OK and return OK

Select box from pyautogui import confirmprint (confirm ('Please select gender', buttons= ['male', 'female']) # will output your clicks

Enter the password from pyautogui import passwordprint (password ('Please enter your password')) # will output the password you just entered

Enter normal content from pyautogui import promptprint (prompt ('Please enter your account')) # will return what you just entered

The above is how to install and use pyautogui in python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report