Pyautogui:我必须手动移动鼠标才能让 pyautogui 单击

Pyautogui: I have to move the mouse manually for pyautogui to click

注意: 我的问题不是它没有点击,我的问题是它只有 点击一次 手动移动鼠标

我正在使用 python 库 'pyautogui',当我 运行 代码如下时:

import pyautogui
pyautogui.FAILSAFE = True
while True:
    cash = pyautogui.locateOnScreen('cash.png',confidence=0.8)
    if cash != None:
        pyautogui.click(cash)

它找到了它要找的东西,但没有点击它直到我手动移动鼠标。

每次我尝试 pyautogui 时都会发生这种情况。

Pyautogui 似乎发送了太多鼠标无法处理的请求,因此通过添加 time.sleep(1) 来减慢它的速度。

我不知道其他人是否遇到过这个问题,但我很高兴它已得到修复!

这可能是因为您没有在“click()”函数中插入坐标。如果您不插入坐标,python 将点击鼠标指针所在的位置。如果你插入像“click(12, 35)”这样的坐标,那么python就会点击你输入的具体坐标。您可以通过任何图像编辑器(例如 paint

)获取屏幕的坐标
>>> import pyautogui

>>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.

>>> currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.

>>> pyautogui.moveTo(100, 150) # Move the mouse to XY coordinates.

>>> pyautogui.click()          # Click the mouse.
>>> pyautogui.click(100, 200)  # Move the mouse to XY coordinates and click it.
>>> pyautogui.click('button.png') # Find where button.png appears on the screen and click it.

>>> pyautogui.move(0, 10)      # Move mouse 10 pixels down from its current position.
>>> pyautogui.doubleClick()    # Double click the mouse.
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)  # Use tweening/easing function to move mouse over 2 seconds.

>>> pyautogui.write('Hello world!', interval=0.25)  # type with quarter-second pause in between each key
>>> pyautogui.press('esc')     # Press the Esc key. All key names are in pyautogui.KEY_NAMES

>>> pyautogui.keyDown('shift') # Press the Shift key down and hold it.
>>> pyautogui.press(['left', 'left', 'left', 'left']) # Press the left arrow key 4 times.
>>> pyautogui.keyUp('shift')   # Let go of the Shift key.

>>> pyautogui.hotkey('ctrl', 'c') # Press the Ctrl-C hotkey combination.

>>> pyautogui.alert('This is the message to display.') # Make an alert box appear and pause the program until OK is clicked.

在你的情况下使用 pyautogui.click(100, 200) # Move the mouse to XY

我会做的是不去

import pyautogui
pyautogui.FAILSAFE = True
while True:
    cash = pyautogui.locateOnScreen('cash.png',confidence=0.8)
    if cash != None:
        pyautogui.click(cash)

你会做

import pyautogui
pyautogui.FAILSAFE = True
while True:
    cash = pyautogui.locateOnScreen('cash.png',confidence=0.8)
    if cash != None:
    pyautogui.move(cash)
        pyautogui.click(cash)

你的问题是程序只是要运行点击但它不知道实际移动到那里然后点击。