**
Python version of the button wizard, you can fish without hands. It's called bottom fishing
**First of all, I am very grateful for the source code idea provided by the up Master of station B "drink water if you are not happy", which is mainly the operation of simulating the operation of keyboard and mouse after finding a picture in python. A script developed mainly based on pyautogui library.
My script is a secondary development based on the source code of Shuige. The main added functions are shown in the figure
The library file is very important. Put it first
import pyautogui import time import xlrd import pyperclip import pyWinhook import keyboard from tkinter import *
Let me briefly introduce the implementation functions:
Function 1:
In the past, Shuige's left click is to automatically move the mouse to the target image and then left click, but sometimes, for example, an input box has no obvious characteristics, or there is a descriptive field in front of it, but you click it and can't trigger the focus event of the web page, that is, you can't input content. I call this function the left button after the relative movement of the mouse. First use 8.0 to move the mouse to the target image, then make your own set relative movement, and then left click. For example, '100 50' means that the mouse moves 100 pixels to the right and 50 pixels to the down. The negative number is in the opposite direction, and the coordinates in the upper left corner of the screen (0, 0) are as long as the picture of the description field in front of the input box is intercepted and then moved relatively.
Serve
#7 represents left click after relative movement of the mouse elif cmdType.value == 7.0: XDMove = sheet1.row(i)[1].value #print(type(XDMove)) is now a string #After splitting the string, it is converted to a number. Get the relative moving distances X and Y strFgf=" " tempX = XDMove[:XDMove.index(strFgf)] tempY = XDMove[XDMove.index(strFgf):] MoveX = int((tempX[1:])) MoveY = int((tempY[:len(tempY)-1])) #Gets the current mouse position initMouseX, initMouseY = pyautogui.position() time.sleep(0.5) pyautogui.click(initMouseX+MoveX,initMouseY+MoveY,2,0.25,button='left') print("Abscissa:"+initMouseX+MoveX,"Ordinate:"+initMouseY+MoveY," Left click once")
Function 2:
Pause function at key position, or single-step debugging can be done. The usage is that the previous automated scripts are too intelligent to carry out manual intervention operations. For example, when we submit a form on a web page, it requires you to enter a verification code, or you need to enter a payment password when paying. At this time, it all depends on the previously set operations, even if it can. You won't rest assured, so you can pause the script and operate it yourself as long as you use this function before this key step. When finished, press F4 and the script will continue with the unfinished steps*
Serve
#10 key position pause function, press F4 to continue the program elif cmdType.value == 10.0: print("Please press F4 Continue running the script") keyboard.wait('F4')
Function III
Hotkey combination, this code I refer to the source code of up master Dayu of station b. Up to 3 keys can be pressed at the same time. It works well with common shortcut keys, such as ctrl+a.
#Define hotkey events #hotkey_ The get method is used to determine the number of hot key combinations and transfer the hot key to the corresponding variable def hotkey_get(hk_g_inputValue): newinput = hk_g_inputValue.split(',') if len(newinput) == 1: pyautogui.hotkey(hk_g_inputValue) elif len(newinput) == 2: pyautogui.hotkey(newinput[0], newinput[1]) elif len(newinput) == 3: pyautogui.hotkey(newinput[0], newinput[1], newinput[2]) #hotkey_ The group method calls hotkey_get method and judge whether its hotkey content needs to be cycled. def hotkeyGroup(hotkey_reTry,hkg_inputValue): if hotkey_reTry == 1: hotkey_get(hkg_inputValue) print("Implemented:",hkg_inputValue) time.sleep(0.1) elif hotkey_reTry == -1: while True: hotkey_get(hkg_inputValue) print("Implemented:",hkg_inputValue) time.sleep(0.1) elif hotkey_reTry > 1: i = 1 while i < hotkey_reTry + 1: hotkey_get(hkg_inputValue) print("Implemented:",hkg_inputValue) i += 1 time.sleep(0.1) # 9 representative_ hot key combination elif cmdType.value == 9.0: # Take the number of retries and cycle. hotkey_reTry = 1 if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0: hotkey_reTry = sheet1.row(i)[2].value inputValue = sheet1.row(i)[1].value hotkeyGroup(hotkey_reTry, inputValue) time.sleep(0.5)
Function 4
Keyboard input monitoring function is set. Every step of its operation and what key is pressed will be recorded. And the start hotkey is set. Press enter to execute. Of course, you can also press the button.
#Keyboard event call detected def onKeyboardEvent(event): print(event.Key) # Returns the key pressed if event.Key == 'Return' : gettimes(event) return True # Create Manager hm = pyWinhook.HookManager() # Monitor keyboard hm.KeyDown = onKeyboardEvent hm.HookKeyboard() root.mainloop()
Function five
UI startup, more intuitive. User defined execution times
def gettimes(event): if u.get() != "" : #Take the value entered by the user and control the number of cycles k=int(u.get()) time.sleep(0.3) root.destroy() j=1 if 0<k<=100: while j <= k : mainWork(sheet1) j+=1 time.sleep(0.1) print("Wait 0.1 second") else: print("Please enter 1~100 Integer between") else: print("Please enter the number of times") #Instantiation window root = Tk() name = Label(root, text="Please enter the number of executions (1)-100 Between)").grid(row=0, column=0) u = StringVar() ent1 = Entry(root, textvariable=u) ent1.grid(row=0, column=1) #submit = Button(root, text = "execute", command = gettimes) grid(row=4, column=4) submit = Button(root, text="implement").grid(row=4, column=4) root.bind('<Button-1>', gettimes)
My code is very low. But it's better to have a UI than not. ha-ha
Write at the end
Non programmer background, python almost zero foundation. I've done php website development for 3 months before. The code level is limited. Don't spray when the boss sees it. My purpose is to generate electricity with love. I come from open source and have to go back to open source. In this way, the community can flourish. You can like, collect and comment. Encourage me.
The source code and exe files have been packaged and transmitted to Baidu network disk. Welcome to learn to use, have better ideas and play. Can comment.
Baidu online disk link:
Link: https://pan.baidu.com/s/1iZ1sFUmm8EnYI9mTdONrOw
Extraction code: wzy6