【问题标题】:Is it possible to get pyautogui to locate images and click in a virtual Machine?是否可以让 pyautogui 定位图像并单击虚拟机?
【发布时间】:2019-09-19 21:26:20
【问题描述】:

我正在尝试创建一个将在虚拟机上执行任务的机器人。其中一些任务涉及浏览网页、单击、键入、在屏幕上定位图像。

我能够在我的工作电脑上成功制作机器人,但我正在努力让 pyautogui 的一些功能在虚拟机上工作。

想知道是否有人在虚拟机中成功使用了pyautogui.locateCenterOnScreenpyautogui.click()?现在我正在使用任务调度程序来启动程序,当我看到程序运行时,鼠标是不可见的,并且在定位图像时卡住了。

itemNotThere = ('itemDoesNotExist.png',.9) #image name and confidence

def check_valid_search(imageName):
    """ Return pixel locations of an image or 1 if not found. """
    r = None
    while r is None:
        try:
            r = pyautogui.locateCenterOnScreen(
                    imageName[0], grayscale=True, confidence=imageName[1])
        except:
            r = 1
    return r

print(check_valid_search(itemNotThere))

【问题讨论】:

  • 如果你想浏览网页,也许你应该使用Selenium,它可以控制网络浏览器,它通常用于浏览网页。
  • 非官方文档链接:selenium-python.readthedocs.io
  • 是的,我不是在寻找 selenium 解决方案,因为我也在寻找在 Web 浏览器之外自动执行任务。我发现你可以使用pyautogui中的locate功能来搜索图像中的图像。我最终做的是截取虚拟机的屏幕截图并使用 y = pyautogui.screenshot() 创建一个图像对象,然后使用 r = pyautogui.locate(imageName[0],y, grayscale=True) 来定位像素我想与之交互的图像的值。

标签: python automation virtual-machine pyautogui


【解决方案1】:

您可以使用pyautogui.screenshot 对虚拟机进行截图,并使用定位功能搜索截图。

itemNotThere = ('itemDoesNotExist.png',.9) #image name and confidence

def check_valid_search(imageName):
    """ Return pixel locations of an image or 1 if not found. """
    r = None
    while r is None:
        try:
            y = pyautogui.screenshot()
            r = pyautogui.locate(imageName[0],y, grayscale=True)
        except:
            r = 1
    return r

print(check_valid_search(itemNotThere))

【讨论】:

    猜你喜欢
    • 2010-10-11
    • 2020-03-30
    • 2011-03-27
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    相关资源
    最近更新 更多