2

如何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()

输出:

<Display cmd_param=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '900x600x24', ':1086'] cmd=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '900x600x24', ':1086'] oserror=None return_code=None stdout="None" stderr="None" timeout_happened=False>
<Display cmd_param=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '900x600x24', ':1087'] cmd=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '900x600x24', ':1087'] oserror=None return_code=None stdout="None" stderr="None" timeout_happened=False>
<Xlib.display.Display object at 0x7f4f5493aa90>
<Xlib.display.Display object at 0x7f4f5493a8d0>

PS
我知道如何点击selenium,但我需要确切的pyautogui事件。

4

2 回答 2

1

这很棘手 - 但有一些巧妙的解决方法:

感谢Abhishek Vaid

要无头运行 PYAUTOGUI,您需要使用 Xdisplay 制作一个 docker,并为 PYAUTOGUI 提供虚拟显示的路径。基本上,它将在后端运行显示内存,而实际上没有显示。服务器可部署。

希望这对你有用(代码可以在上面的链接中找到,虽然我还没有测试过)......

于 2021-04-01T23:17:07.690 回答
0

来自https://pyautogui.readthedocs.io/en/latest/的 pyautogui 文档:

Q: Does PyAutoGUI work on multi-monitor setups.
A: No, right now PyAutoGUI only handles the primary monitor.
于 2020-12-23T13:01:07.367 回答