 * 下載Chrome Driver驅動程式 注意Chrome Driver的版本要與Chrome版本相同 * 安裝/Selenium套件 pip install selenium * 開始程式碼如下: ``` #載入Selenium相關模組 from selenium import webdriver from selenium.webdriver.chrome.options import Options #設定Chrome Driver的執行檔路徑,路徑名稱不能有中文 options=Options() options.chrome_executable_path='D:\Teach\Code\chromedriver.exe' #建立Driver物件實體,用程式操作瀏覽器運作 driver=webdriver.Chrome(options=options) driver.get("http://www.google.com") driver.close() ``` 如果執行後有錯誤訊息,可能是Chrome的版本與下載的Chrome driver不一致,嘗試更新Chrome的版本 * 網頁截圖 ``` #載入Selenium相關模組 from selenium import webdriver from selenium.webdriver.chrome.options import Options #設定Chrome Driver的執行檔路徑,路徑名稱不能有中文 options=Options() options.chrome_executable_path='D:\Teach\Code\chromedriver.exe' #建立Driver物件實體,用程式操作瀏覽器運作 driver=webdriver.Chrome(options=options) driver.maximize_window() #視窗最大化 driver.get("http://www.google.com") driver.save_screenshot("screenshot-google.png") #網頁截圖 driver.get("https:/www.ntu.edu.tw/") driver.save_screenshot("ntu.png") driver.close() ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up