admin管理员组

文章数量:1122846

I'm using an M1 MacBook Pro with docker desktop. So this is a Retina display.

I've deployed the jlesage/firefox image in the following way:

docker run -d \
    --name firefox \
    -p 5800:5800 \
    -p 5900:5900 \
    -e DISPLAY_WIDTH=1024 \
    -e DISPLAY_HEIGHT=768 \
    -e DISPLAY=:0 \
    -v /Users/vw/DEV/docker/scripts:/scripts \
    -e VNC_PASSWORD=password \
    jlesage/firefox

It works fine, I can VNC into it and browse and all.

Next I created a python script with pyautogui to find and click the Facebook button:

import os
import pyautogui
import time

# Ensure the script uses the Docker container's display
os.environ["DISPLAY"] = ":0"

# Delay to allow setup
time.sleep(3)

try:
    # Locate the image within the Docker display
    location = pyautogui.locateCenterOnScreen('Facebook.png', confidence=0.8)

    if location:
        x, y = location
        # Constrain search to 1024x768
        if 0 <= x < 1024 and 0 <= y < 768:
            print(f"Image found at: {location}")
            pyautogui.click(location)
        else:
            print(f"Image found outside bounds (1024x768): {location}")
    else:
        print("Image not found on the screen.")
except Exception as e:
    print(f"An error occurred: {e}")

The error message I get is: Image found outside bounds (1024x768): Point(x=np.int64(1743), y=np.int64(1145))

so it's not using the display I'm specifying.

I've also exported DISPLAY manually from my terminal.

本文标签: