如何将pyautogui附加到虚拟显示?

2024-04-16 17:47:30 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在多线程模式下将pyautogui附加到显示器?在

在我的代码示例中,pyautogui始终可以访问上部显示。在

是否可以在每个显示器上都有pyautogui控件?在

import os
from selenium import webdriver
from pyvirtualdisplay import Display
import Xlib.display
# ...

# let's say i run this function in two threads
def do_work(data):
    v_display = Display(visible=0, size=(900, 600))
    v_display.start()

    # How can i attach v_display to the pyautogui?
    import pyautogui

    print(v_display)
    # https://stackoverflow.com/questions/35798478/how-i-can-attach-the-mouse-movement-pyautogui-to-pyvirtualdisplay-with-seleniu
    pyautogui._pyautogui_x11._display = Xlib.display.Display(os.environ['DISPLAY'])

    print(pyautogui._pyautogui_x11._display)

    chrome_options = Options()
    chrome_options.add_argument('--user-data-dir='+str(data['profile']))
    chrome_options.add_argument("--start-maximized")


    driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options)

    driver.get('https://my_resource.co/?'+data['param'])


    pyautogui.click(x=880, y=580)
    # Click always goes through the upper display
    # I also tried to take screenshots they are always the same

    # ...

    driver.quit()
    v_display.stop()

输出:

^{pr2}$

备注:
我知道如何通过selenium进行点击,但我需要pyautogui事件。在


Tags: thetofromimportdataosdriverselenium