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

不打扰是莪最后的温柔 提交于 2019-12-20 14:24:03

问题


I am trying to automatize a website how have a SWF inside.

I cant move the mouse with selenium, because is a SWF,so 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 still see how pyautogui move my mouse.

My example code:

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 I can attach the mouse to the pyvirtualdisplay instance?


回答1:


You can monkey-patch pyautogui internals. Tested on '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()

Note: this should be sufficient to enable pyautogui mouse, using keyboard may require additional configuration of key mapping. For more info, please see: https://github.com/asweigart/pyautogui/blob/master/pyautogui/_pyautogui_x11.py



来源:https://stackoverflow.com/questions/35798478/how-i-can-attach-the-mouse-movement-pyautogui-to-pyvirtualdisplay-with-seleniu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!