Skip to content

Instantly share code, notes, and snippets.

@BroHui
Last active May 31, 2016 09:57
Show Gist options
  • Save BroHui/d92c56ce6de0ac3d8581fe7ee3139ef3 to your computer and use it in GitHub Desktop.
Save BroHui/d92c56ce6de0ac3d8581fe7ee3139ef3 to your computer and use it in GitHub Desktop.
Headless Chrome via Xvfb & selenium
from pyvirtualdisplay import Display
from selenium import webdriver
import os
display = Display(visible=0, size=(1024, 768))
# display port ,exmaple :1001
displayport = display.display
os.environ["DISPLAY"] = displayport
display.start()
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--user-data-dir")
import pdb
try:
browser = webdriver.Chrome(chrome_options=options)
#browser = webdriver.Chrome()
#browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.page_source
browser.quit()
finally:
display.stop()
PyVirtualDisplay==0.2
selenium==2.53.2
yum install -y xorg-x11-server-Xvfb libexif libexif-devel gtk2-devel
# get Chrome 50
wget http://orion.lcg.ufrj.br/RPMS/myrpms/google/google-chrome-stable-50.0.2661.94-1.x86_64.rpm
rpm -ivh google-chrome-stable-50.0.2661.94-1.x86_64.rpm
# get chromedriver 2.21(lastest) (Only support Chrome46-50)
# http://chromedriver.storage.googleapis.com/index.html?path=2.21/
wget http://chromedriver.storage.googleapis.com/2.21/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
cp -v chromedriver /usr/local/bin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment