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)
浏览器 | 驱动下载地址 |
---|---|
firefox | geckodriver |
chrome | chromedriver |
下载对应的系统版本 ,配置环境变量。
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
(<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="0e1243da-8f57-4a41-8a21-50c463e51515", element="83e12db3-3b9f-4717-868c-2a5f22a85844")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="0e1243da-8f57-4a41-8a21-50c463e51515", element="83e12db3-3b9f-4717-868c-2a5f22a85844")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="0e1243da-8f57-4a41-8a21-50c463e51515", element="83e12db3-3b9f-4717-868c-2a5f22a85844")>)
[root@vil-host1 python]#
这样就OK了,如果显示其他的就好好检查一下环境是否配置正确。
网友评论