如何使用Selenium Webdriver(python)将鼠标移动(pyautogui)附加到pyvirtualdisplay? [英] How I can attach the mouse movement (pyautogui) to pyvirtualdisplay with selenium webdriver (python)?
问题描述
我正在尝试使网站内部自动化SWF.
I am trying to automatize a website how have a SWF inside.
我不能用硒来移动鼠标,因为它是SWF,所以要解决这个问题,我使用pyautogui库.
I cant move the mouse with selenium, because is a SWF,so to fix this I use the pyautogui library.
一切正常!但是!当我使用pyvirtualdisplay隐藏导航器时,鼠标没有连接,所以我仍然看到pyautogui如何移动鼠标.
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.
我的示例代码:
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()
如何将鼠标连接到pyvirtualdisplay实例?
How I can attach the mouse to the pyvirtualdisplay instance?
推荐答案
您可以猴子修补pyautogui内部构件.在"xvfb"后端进行了测试.
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()
注意:这应该足以启用pyautogui鼠标,使用键盘可能需要对键映射进行其他配置. 有关更多信息,请参见: > https://github.com/asweigart/pyautogui/blob/master/pyautogui /_pyautogui_x11.py
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
这篇关于如何使用Selenium Webdriver(python)将鼠标移动(pyautogui)附加到pyvirtualdisplay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!