【问题标题】:Python: PyAutoGui click location is off by a few pixels when using an image to locatePython:使用图像定位时,PyAutoGui 点击位置偏离了几个像素
【发布时间】:2020-05-11 22:56:12
【问题描述】:

程序目标:打开一个网络浏览器标签到 youtube,使用 Youtube 主屏幕上“Youtube”按钮的保存图像将鼠标位置移动到该位置,做一个鼠标点击那里

问题: 执行 click() 步骤时,鼠标移动到偏离几个像素(-29 x,-35 y)的位置。在 locateCenterOnScreen 时坐标是正确的,但在 click() 时坐标不同

我尝试过的:我让程序在获取图片位置时打印出图片的坐标,并且当时坐标正确,我使用鼠标定位程序缩小多少。

我的问题:是什么导致 click() 的位置偏移了这几个像素,我该如何解决?

import pyautogui as auto
import webbrowser
import time


site = "https://www.youtube.com/"
webbrowser.open_new_tab(site)
time.sleep(5)
x, y = auto.locateCenterOnScreen('test.png')
print(x)
print(y)
try:
    auto.click(x,y)
except:
    print("Not Found")

【问题讨论】:

    标签: python-3.x pyautogui


    【解决方案1】:

    我最终重新拍摄了用于程序定位的照片,它现在可以工作了。我不确定为什么原来的那个没有按预期工作。

    【讨论】:

      【解决方案2】:

      您的窗口可能已调整大小,因此您要查找的图像的宽度和高度也发生了变化。 我建议使用:

      win = pygetwindow.getWindowsWithTitle('windownname')[0]
      win.size = (1600, 900)
      

      调整窗口大小

      【讨论】:

        猜你喜欢
        • 2020-02-13
        • 2018-09-08
        • 2021-07-15
        • 2023-01-30
        • 2019-06-07
        • 1970-01-01
        • 2020-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多