2017年12月26日星期二

Selenium + ChromeDriver + Xvfb run on ubuntu server without X11

Install Chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -i google-chrome-stable_current_amd64.deb
apt-get update
apt-get install -f
Install pip
apt-get install python-pip
Install Selenium
export LC_ALL=C
pip install selenium 
Install Xvfb
apt-get install xvfb
Download ChromeDriver
wget https://chromedriver.storage.googleapis.com/2.34/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
 Run Xvfb
Xvfb :22

Test selenium
attention: google chrome will crash if running under root user. Switch user first  if your are root.
export DISPLAY=:22
python
from selenium import webdriver
driver = webdrvier.Chrome("./chromedriver")
driver.get("http://www.baidu.com")

#should return https protocl url
driver.execute_script("return window.location")

#should return an element
driver.execute_script("return document.getElementById('kw')")