Skip to content

Instantly share code, notes, and snippets.

@e96031413
Last active April 4, 2021 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e96031413/70b4c56af66e8e2f659c73c97cfaa5b8 to your computer and use it in GitHub Desktop.
Save e96031413/70b4c56af66e8e2f659c73c97cfaa5b8 to your computer and use it in GitHub Desktop.
2021/01/24在無GUI介面的Server上執行Selenium(以aiForge為例)
#安裝chromium-browser
sudo apt-get install chromium-browser
dpkg --configure -a
apt-get install -f
chromium-browser -version #記住版本號,待會要下載對應的driver
#安裝Selenium和xvfb
pip install selenium
sudo apt-get install xvfb
#下載chromedriver並解壓縮到/usr/bin
wget https://chromedriver.storage.googleapis.com/89.0.4389.23/chromedriver_linux64.zip
unzip chromedriver_linux64.zip && rm chromedriver_linux64.zip
mv chromedriver /usr/bin/
sudo /usr/sbin/service cron start
# -----------------------------------------------------------------------------------------
#程式碼關鍵點在於使用
#option.binary_location = '/usr/bin/chromium-browser'找到driver位置、headless模式、disable-gpu不使用GPU、no-sandbox無沙盒模式,這三個選項,其餘寫法與一般使用相同。
from selenium import webdriver
option = webdriver.ChromeOptions()
option.binary_location = '/usr/bin/chromium-browser'
option.add_argument('--headless')
option.add_argument('--disable-gpu')
option.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=option)
driver.get('https://yanwei-liu.medium.com/')
print(driver.title)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment