Selenium-Python客户端库-在后台自动化
我正在尝试使用Selenium-Python Client
Library自动执行到Web应用程序的登录过程。最终目标是学习Selenium的Python客户端库。因此,我真的很感谢Selenium-
Python的那些人的答案。
我目前有这样的代码:
from selenium import webdriverfrom selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://myServer/WebAccess/login.html") # Load Application page
elem = browser.find_element_by_name("LoginID") # Find the Login box
elem.send_keys("Administrator")
elem = browser.find_element_by_name("Password") # Find the Password box
elem.send_keys("Administrator" + Keys.RETURN)
这很好,但是所有操作都在前端进行。我的意思是,它实际上是打开Firefox,键入值,单击Submit等,这与预期的一样。
我只是想知道,我能做些什么使所有这些事情在后台发生吗?让我们说我不想监视脚本在做什么。我只希望它在后台运行。有什么办法可以做到这一点?
下载PyVirtualDisplay并使用命令将其安装在Windows中setup.py install
。也安装了EasyProcess和Path模块。
现在我有一个示例代码
from pyvirtualdisplay import Displayfrom selenium import webdriver
display = Display(visible=0, size=(1024, 768))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.close()
display.stop()
我在执行此代码时遇到以下错误:
`Traceback (most recent call last): File "C:\Documents and Settings\user\Desktop\Sel.py", line 1, in <module>
from pyvirtualdisplay import Display
File "C:\Python27\lib\site-packages\pyvirtualdisplay\__init__.py", line 1, in <module>
from display import Display
File "C:\Python27\lib\site-packages\pyvirtualdisplay\display.py", line 2, in <module>
from pyvirtualdisplay.xephyr import XephyrDisplay
File "C:\Python27\lib\site-packages\pyvirtualdisplay\xephyr.py", line 8, in <module>
EasyProcess([PROGRAM, '-help'], url=URL, ubuntu_package=PACKAGE).check_installed()
File "C:\Python27\lib\site-packages\easyprocess\__init__.py", line 202, in check_installed
raise EasyProcessCheckInstalledError(self)
EasyProcessCheckInstalledError: cmd=['Xephyr', '-help']
OSError=[Error 2] The system cannot find the file specified
Program install error! `
回答:
Firefox(和其他图形浏览器)需要X显示器。您可以在PyVirtualDisplay的帮助下使用虚拟的:
from pyvirtualdisplay import Displaydisplay = Display(visible=0, size=(1024, 768))
display.start()
browser = webdriver.Firefox()
... more selenium code ...
display.stop()
除了PyVirtualDisplay,您还需要其依赖项xfvb和Xephyr(在debian:上apt-get install -y xvfb
xserver-xephyr)
以上是 Selenium-Python客户端库-在后台自动化 的全部内容, 来源链接: utcz.com/qa/415011.html