How can I attach mouse movement (pyautogui) to pyvirtualdisplay with selenium webdriver (python)?

I am trying to automate a website like inside SWF.

I cannot move the mouse with selenium because it is SWF, therefore, to fix this, I use the pyautogui library.

Everything works fine !, but! when I use pyvirtualdisplay to hide the navigator, the mouse is not attached, so I can still see how pyautogui moves the mouse.

My sample code is:

from selenium import webdriver
from pyvirtualdisplay import Display
import pyautogui

display = Display(visible=1, size=(1600,900))
display.start()


driver = webdriver.Firefox()
driver.set_window_size(1600,900)
driver.get('https://website.where.I.have.the.SWF.com')

sleep(5)
pyautogui.click(450, 180)

driver.close()
display.stop()

How can I attach a mouse to a pyvirtualdisplay instance?

+5
source share
2 answers

You can monkey patch Pyautogui internal organs. Tested on the 'xvfb' backend.

import os
from pyvirtualdisplay import Display
import pyautogui
import Xlib.display

v_display = Display(visible=1, size=(1600,900))
v_display.start()  # this changes the DISPLAY environment variable
# sadly, pyautogui does not detect this change
pyautogui._pyautogui_x11._display = Xlib.display.Display(
                os.environ['DISPLAY']
            )
...
pyautogui.click(...)  # clicks on v_display
...

v_display.stop()

: pyautogui, . , , : https://github.com/asweigart/pyautogui/blob/master/pyautogui/_pyautogui_x11.py

+4

, DISPLAY pyautogui. . DISPLAY , ==> Xlib.error.DisplayConnectionError: ": 1": [Errno 2] . , . - .

0

All Articles