1.安装Xvfb和pyvirtualdisplay
sudo apt-get install xvfb
pip install pyvirtualdisplay
2.安装chrome, chromedriver和selenium
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb
wget https://chromedriver.storage.googleapis.com/75.0.3770.90/chromedriver_linux64.zip
解压,将chromedriver加到path中
pip install selenium
到这里。环境就配置好了。接下来就可以上代码了。
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(900, 800))
display.start()
driver = webdriver.Chrome()
driver.get('http://www.umn.edu')
print (driver.title)