selenium+Headless Chrome實現不彈出瀏覽器自動化登陸

目前因爲phantomjs已經不維護了,而新版的Chrome(59+)推出了Headless模式,對爬蟲來講尤爲是定時任務的爬蟲截屏之類的是一大好事。python

不過按照網絡上的一些方法來寫的話,會報下面的錯誤:linux

 

後來通過分析,他們運行python是在mac或者linux下進行的,win下因爲高版本的chromedriver只能經過路徑進行指定,因此會出現這類找不到驅動程序的錯誤。web

通過比對常識網絡上的各類代碼,後來得出了win下可順暢執行的driver的寫法以下:chrome


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image,ImageEnhance


path = 'E:/Cyou/chromedriver.exe'
瀏覽器

 


#打開瀏覽器
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 驅動路徑
path = 'E:/Cyou/chromedriver.exe'
# 建立瀏覽器對象
driver = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)
注意:別忘了導入:from selenium.webdriver.chrome.options import Options網絡

不然會報錯。less

而後後面就能夠進行以前的邏輯不進行改動了,只要這裏書寫正確就能夠了。.net

問題解決。
---------------------
原文:https://blog.csdn.net/qq_26803795/article/details/82467037

對象

相關文章
相關標籤/搜索