目标
在centos6上部署selenium with firefox driver
系统默认配置
lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 6.9 (Final)
Release: 6.9
Codename: Final
python --version
Python 2.6.6
最终配置
Python 2.7.14
centos 6.9
selenium 3.8.1
geckodriver 0.16.1
firefox 45.9.0
说明 (> 45 versions depends on gtk3, which depends on gnome 3 while centos6 depends on gnome 2, and need to set capabilities["marionette"] = False
详细步骤
确认DNS配置,以及可以访问外网
cat /etc/resolv.conf
search XXXX.com
nameserver X.X.X.X
nameserver Y.Y.Y.Y
options timeout:2
options rotate
配置yum源 并 升级yum本身
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
or
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
yum clean
yum makecache
yum upgrade
(需要确认一次)
安装开发者工具包
yum -y install http://linuxdownload.adobe.com/linux/x86_64/adobe-release-x86_64-1.0-1.noarch.rpm
yum install flash-plugin (需要确认)
yum -y groupinstall "X Window System" "Desktop" "Fonts" "General Purpose Desktop"
yum install -y ImageMagick
yum install -y Xvfb
安装python2.7最新版
安装前,系统自带的python是2.6.6,需要安装2.7最新版。
安装python 2.7
# 下载python 2.7 并编译,安装
wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
tar -xzvf Python-2.7.14.tgz
cd Python-2.7.14
./configure
make && make install
# 默认 Python 2.7.14 会安装在 /usr/local/bin 目录下。
ll -tr /usr/local/bin/python*
/usr/local/bin/python2.7
/usr/local/bin/python2.7-config
/usr/local/bin/python -> python2
/usr/local/bin/python2 -> python2.7
/usr/local/bin/python2-config -> python2.7-config
/usr/local/bin/python-config -> python2-config
#而系统自带的 Python 是在 /usr/bin 目录下。
ll -tr /usr/bin/python*
/usr/bin/python2.6-config
/usr/bin/python2.6
/usr/bin/python
/usr/bin/python2 -> python
/usr/bin/python-config -> python2.6-config
更新系统默认 Python 版本
# 先把系统默认的旧版 Python 重命名。
mv /usr/bin/python /usr/bin/python.old
# 再删除系统默认的 python-config 软链接。
rm -f /usr/bin/python-config
# 最后创建新版本的 Python 软链接。
ln -s /usr/local/bin/python /usr/bin/python
ln -s /usr/local/bin/python-config /usr/bin/python-config
ln -s /usr/local/include/python2.7/ /usr/include/python2.7 (可选,我没选)
# 以上步骤做完以后,目录 /usr/bin 下的 Python 应该是
ll -tr /usr/bin/python*
/usr/bin/python2.6-config
/usr/bin/python2.6
/usr/bin/python.old
/usr/bin/python2 -> python
/usr/bin/python -> /usr/local/bin/python
/usr/bin/python-config -> /usr/local/bin/python-config
# 查看新的 Python 版本,python --version,返回 Python 2.7.14 为正常。
为新版 Python 安装 setuptools
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
setuptools 正确安装完成后,easy_install 命令就会被安装在 /usr/local/bin 目录下了。
注意:
升级 Python 可能会导致 yum 命令不可用。解决方法如下:
编辑 /usr/bin/yum 文件,将开头第一行的
#!/usr/bin/python
改为
#!/usr/bin/python2.6
安装pip,Pillow, pyscreenshot, pyvirtualdisplay, selenium
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install Pillow
pip install pyscreenshot
pip install pyvirtualdisplay
pip install -U selenium -f /usr/bin/python-config
安装firefox 45.9.0
wget wget https://ftp.mozilla.org/pub/firefox/releases/45.9.0esr/linux-x86_64/zh-CN/firefox-45.9.0esr.tar.bz2 -O firefox-45.9.0esr-zh-CN.tar.bz2
tar -xf firefox-45.9.0esr-zh-CN.tar.bz2
mv firefox /opt/firefox-45.9.0esr-zh-CN
rm -f /usr/bin/firefox
ln -s /opt/firefox-45.9.0esr-zh-CN/firefox-bin /usr/bin/firefox
>firefox --version
Mozilla Firefox 45.9.0
是时候测试一下了
python test.py
输出包括网页源码,网页截图保存为pycm.png
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(1024, 768))
display.start()
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = False
browser = webdriver.Firefox(capabilities=capabilities)
browser.get('http://www.baidu.com/')
browser.get_screenshot_as_file('./bd.png')
print browser.page_source
browser.close()
display.stop()
sz -bye bd.png看下截图是不是符合预期就可以了。
参考文档
- Install headless Firefox on CentOS 6 for Selenium automation
- centos 6.9 安装python 2.7.14
- selenium, geckodriver 与 Firefox 版本不匹配
- Releases mozilla/geckodriver Github
后记
其他错误
中间安装tkinter重新编译安装了一次python,导致import numpy什么的报找不到module
查看一下默认python目录:
rm -f /usr/bin/python
rm -f /usr/bin/python-config
ln -s /usr/local/bin/python-config /usr/bin/python-config
ln -s /usr/local/bin/python /usr/bin/python
pip无法运行
pip
Traceback (most recent call last):
File "/usr/bin/pip", line 7, in <module>
from pip import main
ImportError: No module named pip
重新安装了一下pip
然后重装了numpy, pandas, matplotlib, selenium
pip install -U numpy
pip install -U pandas
pip install -U matplotlib
pip install -U selenium
pip install -U Pillow
pip install -U pyscreenshot
pip install -U pyvirtualdisplay
网友评论