user3238431
user3238431

Reputation: 1

Is it possible to get pyautogui to locate images and click in a virtual Machine?

I am trying to create a bot that will perform tasks on a virtual machine. Some of the tasks involve navigating a web page, clicking, typing, locating images on the screen.

I was able to successfully make the bot on my work pc but I am struggling to get some of the functions from pyautogui to work on a virtual machine.

Was wondering if anyone had success using pyautogui.locateCenterOnScreen or pyautogui.click() in a Virtual machine? For now I am using task scheduler to start the program and when I watch the program run the mouse is invisible and it is getting stuck locating images.

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))

Upvotes: 0

Views: 955

Answers (2)

Charles Hartman
Charles Hartman

Reputation: 1

If you want to do this for a VM, you should install python and the corresponding modules to the VM (so you may have to install python each time you use the VM software, depending on the approach) and executing the script from the VM environment. The end result should be that the window used for the VM environment should act as the display, and it should be able to read it the same way as if you used it on your primary OS.

Upvotes: 0

user3238431
user3238431

Reputation: 1

You can take a screenshot of the virtual machine with pyautogui.screenshot and search the screenshot with the locate function.

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))

Upvotes: 0

Related Questions