Python練習:
目的:利用chrome開啟網頁並將網頁截成圖片
利用套件有:selenium、logging
執行前需先下載chromedriver元件
#取得網頁最大化的畫面(與捲軸無關)
from selenium import webdriver
import logging
from selenium.webdriver.chrome.options import Options
try :
#chromedriver元件路徑
chromedriver = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
chrome_options = Options()
#argument參考:http://www.assertselenium.com/java/list-of-chrome-driver-command-line-arguments/
chrome_options.add_argument("--start-maximized")
driver=webdriver.Chrome(executable_path=chromedriver,chrome_options=chrome_options)
#要截圖的網站
driver.get('你要開的網頁網址')
driver.save_screenshot('image.png')
except Exception as e:
logging.basicConfig(filename='logging.txt')
logging.warning(datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
logging.warning(e)#"element is not exist");
finally:
driver.close()