1、Python 环境安装
安装Python
[root@xxxx]# yum install python
[root@xxxx]# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
[root@xxxx]# python get-pip.py
2、配置无图形化环境
安装Xvfb和pyvirtualdisplay
[root@xxxx]# yum install xorg-x11-server-Xvfb
[root@xxxx]# pip install pyvirtualdisplay
安装firefox和selenium
[root@xxxx]# yum install firefox
[root@xxxx]# pip install selenium
安装 selenium 浏览器驱动(我这里使用 firefox)
下载对应的系统版本 ,配置环境变量。
3、脚本测试 test.py
编写脚本 test.py
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
display = Display(visible=0, size=(900, 800))
display.start()
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
执行脚本
[root@xxxx]# python test.py
(, , )
[root@vil-host1 python]#
这样就OK了,如果显示其他的就好好检查一下环境是否配置正确。