1.搭建web自动化环境需要对应版本的firefox,selenium以及浏览器驱动,三者对应关系可查询https://github.com/mozilla/geckodriver/releases/
2.经过楼主测试以及查阅资料发现firefox 48以下的版本不需要安装驱动 对应selemium2,我们也知道Ubuntu是自带firefox。目前,我刷出的系统firefox也是45.0.2的版本,系统信息如下:
root@tom:~$ uname -a
Linux pi-ThinkPad-X220 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:34:49 UTC 2016 i686 i686 i686 GNU/Linux
root@tom:~$ cat /etc/os-release
NAME=“Ubuntu”
VERSION=“16.04 LTS (Xenial Xerus)”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=“Ubuntu 16.04 LTS”
VERSION_ID=“16.04”
HOME_URL=“http://www.ubuntu.com/”
SUPPORT_URL=“http://help.ubuntu.com/”
BUG_REPORT_URL=“http://bugs.launchpad.net/ubuntu/”
UBUNTU_CODENAME=xenial
3.也可以更换软件源下载火狐浏览器
1)sudo apt-cache madison firefox–查看各个源安装的firefox版本
2)sudo nano /etc/apt/sources.list–编辑源
3)sudo apt-get update–更新源
4)sudo apt-get install firefox–下载对应源的火狐,然后查看火狐版本
4.pip安装selenium以及相关类库
1)安装pip:sudo apt-get install python-pip python-dev build-essential
2)sudo -H pip install pyvirtualdisplay
3)sudo -H pip install selenium==2.53.1
5.测试代码
from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.support.ui import WebDriverWait
class webControl:
url = r'http://www.baidu.com'
def testDemo():
try:
# 相当于画布承载网页
display = Display(visible=0, size=(800, 600))
display.start()
print('当前正在访问: %s' % url)
driver = webdriver.Firefox()
# 设置网页加载报错时间
driver.set_page_load_timeout(15)
current_time = float(time.time())
driver.get(url)
openweb_time = float(time.time()) - current_time
# 网页标题,用于判断网页是否打开
str = driver.title
except:
str = ''
finally:
if "百度一下" in str:
print('打开网页的时间为:%ss' % ((i + 1), openweb_time))
else:
print('打开网页失败')
display.stop()
driver.quit()
if __name__ == '__main__':
webControl.testDemo()
当然,你也可以直接在终端里测试!