pip install selenium
因為phantomJS將停止維護,所以建議使用headless chrome
ChromeDriver is a separate executable that WebDriver uses to control Chrome.
1、確保谷歌瀏覽器安裝在可以找到的位置(默認位置或自己指定的位置)。
如果不是默認位置,則需要用下面的代碼來指定谷歌瀏覽器的安裝位置:
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");
2、下載你系統上所需要的ChromeDriver文件,windows所需下載地址為:
https://chromedriver.storage.googleapis.com/index.html?path=2.35/
3、幫助WebDriver找到你下載的ChromeDriver文件:
將ChromeDriver文件存放在PATH目錄下或
或
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver')
4、(可選)啟動和退出ChromeDriver server需要一些時間,所以提供了兩種方法來
解決這個問題:
1、使用ChromeDriverService
2、作為一個服務器單獨啟動ChromeDriver server,然后用Remote WebDriver連接它。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.baidu.com")
print(driver.title)
driver.quit()
參考鏈接:
https://sites.google.com/a/chromium.org/chromedriver/home 介紹地址
https://sites.google.com/a/chromium.org/chromedriver/getting-started 入門地址