1.安裝chrome
yum install google-chrome
2.安裝chromedriver
所有版本的下載地址:https://sites.google.com/a/chromium.org/chromedriver/downloads
wget https://chromedriver.storage.googleapis.com/2.39/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
3.selenium
pip install selenium
4.例子
>>> from selenium import webdriver >>> chrome_options = webdriver.ChromeOptions() >>> chrome_options.add_argument('--headless') >>> chrome_options.add_argument('--no-sandbox') # 這句一定要加 #我一開始沒加,就一直報‘selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally’。搞了很久。 >>> driver = webdriver.Chrome(executable_path='/root/chromedriver', chrome_options=chrome_options) >>> driver.get('http://py2app.readthedocs.io/en/latest/examples.html') >>> print driver.title Example setup.py templates — py2app 0.15 documentation
5.注意
chromedriver應該要給執行權限,也就是chmod +x chromedriver,因為我為了解決Chrome failed to start: exited abnormally這個錯誤,做了很多嘗試,也不知道這個是不是必須的,加了不會錯😄