2

How do I attach pyautogui to the display in multithreaded mode?

In the example of my code, pyautogui always has access to the upper display.

Is it possible to have pyautogui control on each display?

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

Output:

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

P.S.
I know how to make clicks through selenium, but I need exactly pyautogui events.

martineau
  • 119,623
  • 25
  • 170
  • 301
slaw
  • 55
  • 1
  • 10
  • [James](https://stackoverflow.com/users/13607672) wrote an [Answer](https://stackoverflow.com/a/66085601) saying "Well, what about this source? https://abhishekvaid13.medium.com/pyautogui-headless-docker-mode-without-display-in-python-480480599fc4 (need to have at least 30 chars!!)" – Scratte Feb 08 '21 at 04:43
  • Thanks Scratte - big debate with admin team who kept hiding / deleting my post on the premise the link may change - despite the fact that I archived the page in 'Wayback Chrome'. Latest below doesn't seem to be causing them too much fuss- so all good for now :) – JB-007 Apr 05 '21 at 06:19

2 Answers2

1

This is tricky to do - but there are some ingenious workarounds:

Thanks to Abhishek Vaid:

"To run PYAUTOGUI headless you will need to make a docker using Xdisplay and provide path to virtual display to PYAUTOGUI to do so. Basically it will have the display memory run in the backend without actually having a display. Server deployable."

Hope this works for you (code can be found at link above, I haven't tested though)...

JB-007
  • 2,156
  • 1
  • 6
  • 22
0

From pyautogui documentation at https://pyautogui.readthedocs.io/en/latest/ :

Q: Does PyAutoGUI work on multi-monitor setups.
A: No, right now PyAutoGUI only handles the primary monitor.