selenium 在linux上无界面运行,其实是非常简单的。

具体的方法有使用HtmlUnitDriver或者PhantomJSDriver,其实基本和ChromeDriver 和FirefoxDriver是一样的。但是有些人或者会比较排斥他们说HtmlUnitDriver对JS支持不好,PhantomJSDriver估计也很少有人用,其实他是对Phantomjs的封装,对这些不做多过评论,用下来感觉还好。

还有另一种方法,就是使用Xvfb, 有人说Xvfb是什么,没听说过,没听说过就自己Google吧。

这里就主要是讲一下Xvfb的安装使用。以chrome + ubuntu 和 firefox + centOS 为例子(chrome linux版好像是到6的时候就不支持centOS了, 都自带firefox )

 

一、 Xvfb在Ubuntu上的安装(chrome)

1. 安装ubuntu(百度google安装步骤)

2. putty.exe 连接ubuntu

安装openssh-server: sudo apt-get install openssh-server
启动openssh-server: sudo /etc/init.d/ssh start
确认openssh-server是否启动:ps -e | grep ssh
telnet ip 端口号

3. 安装oracle JDK6 (可跳过:自带penjdk-6-jre)
$ remove openjdk:  sudo apt-get autoremove openjdk-6-jre
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java6-installer
$ sudo update-java-alternatives -s java-6-oracle

4. 装chrome (自带firefox)
http://www.ubuntuupdates.org/ppa/google_chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - 
sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update 
sudo apt-get install google-chrome-stable

5. 装Xvfb 及各种
sudo apt-get update && sudo apt-get install -y xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic Xvfb x11-apps  imagemagick firefox google-chrome-stable

OK, 到这一步都装好了

 

二、测试安装

putty连接Ubuntu运行下面命令:

1. 启动Xvfb服务

 Xvfb -ac :7 -screen 0 1280x1024x8  (注意这个是x, 不是* 哦)

 2.  启动firefox or chrome

export  DISPLAY=:7

/usr/bin/google-chrome-stable http://www.mimvp.com     // chrome 浏览www.mimvp.com

或者

export DISPLAY=:7

firefox http://www.mimvp.com                                          // firefox 浏览www.mimvp.com

如果运行完后,出现:

Xlib: extension "RANDR"  missing on display ":7"

you made it. 这是个无关紧要的信息,之前我试图解决掉它,没有成功。最后我在运行selenium脚本后,完全没有出现这个信息,脚本执行很正常,所以现在我把它当做是安装成功的信息。

 

当然运行selenium 脚本前总不能老是敲一遍这些命令,太麻烦了。

弄成一个服务,先 touch /etc/init.d/Xvfb

脚本如下:

#! /bin/bash
if [ -z "$1" ]; then 
echo "`basename $0` {start|stop}"
exit
fi

case "$1" in
start)
      /usr/bin/Xvfb :7 -ac -screen 0 1024x768x8 &
;;
stop)
      killall Xvfb
;;
esac

修改脚本权限,启动服务:

chmod +x /etc/init.d/Xvfb
chkconfig Xvfb on
service Xvfb start

停止服务的话就是: service Xvfb stop

完毕了。

测试脚本:

sudo apt-get install xvfb
sudo pip install pyvirtualdisplay


from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')
print browser.page_source

browser.close()
display.stop()

 

三、Xvfb在CentsOS安装

Install Xvfb with library:

    yum install Xvfb
    yum -y install libXfont
    yum install xorg-x11-fonts*


touch /etc/init.d/Xvfb with content:

#!/bin/bash
#
# /etc/rc.d/init.d/Xvfbd
#
# chkconfig: 345 95 28
# description: Starts/Stops X Virtual Framebuffer server
# processname: Xvfb
#

. /etc/init.d/functions

[ "${NETWORKING}" = "no" ] && exit 0

PROG="Xvfb"
PROG_OPTIONS=":7 -ac -screen 0 1024x768x24"
PROG_OUTPUT="/tmp/Xvfb.out"

case "$1" in
    start)
        echo -n "Starting : X Virtual Frame Buffer "
        $PROG $PROG_OPTIONS>>$PROG_OUTPUT 2>&1 &
        disown -ar
        /bin/usleep 500000
        status Xvfb & >/dev/null && echo_success || echo_failure
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            /bin/touch /var/lock/subsys/Xvfb
            /sbin/pidof -o  %PPID -x Xvfb > /var/run/Xvfb.pid
        fi
        echo
        ;;
    stop)
        echo -n "Shutting down : X Virtual Frame Buffer"
        killproc $PROG
        RETVAL=$?
        [ $RETVAL -eq 0 ] && /bin/rm -f /var/lock/subsys/Xvfb /var/run/Xvfb.pid
        echo
        ;;
    restart|reload)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
    status)
        status Xvfb
        RETVAL=$?
        ;;
    *)
     echo $"Usage: $0 (start|stop|restart|reload|status)"
     exit 1
esac

exit $RETVAL

Registering in system and start:
chmod +x /etc/init.d/Xvfb
chkconfig Xvfb on
service Xvfb start

now

export DISPLAY=:7 (actually you should add this to your etc/bashrc)

 

参考资料:

Python+Selenium2 搭建自动化测试环境 (推荐

selenium/appium 学习系列教程 (推荐

how-to-run-selenium-headless-firefox-in-ubuntu

running-headless-webdriver-based-selenium-junit-tests-inside-jenkins-under-ubuntu-linux

Xlib: extension “RANDR” missing on display “:21”. - Trying to run headless Google Chrome

http://rrroutes.blogspot.com/2013/04/settup-environment-for-selenium-tests.html

error with Firefox Binary 'The browser appears to have exited'

The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details