Python webdriver.Chrome()的使用解讀
webdriver.Chrome()的使用
1.前提
Python與Chrome路徑下均安裝chromedriver.exe。
2.chromedriver.exe版本選擇及下載
- 下載地址為:http://npm.taobao.org/mirrors/chromedriver/
- Chrome版本查看:瀏覽器右上角三個點->幫助->關(guān)于Google Chrome

chromedriver.exe版本需要與瀏覽器版本一致:

3.安裝
下載后解壓,將 chromedriver.exe復(fù)制到下面兩個目錄中:
- Chrome目錄:比如
C:\Program Files (x86)\Google\Chrome\Application - Python目錄:比如
D:\Softwares\Python39
4.添加環(huán)境變量
將上述Chrome路徑添加進(jìn)系統(tǒng)環(huán)境光變量,Python使用時應(yīng)該加入環(huán)境變量了,這個就不用管了。
5.測試代碼
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id("kw").send_keys(u"胡皓天")
driver.find_element_by_id("su").click()
webdriver.Chrome參數(shù)解釋
通過源碼看解釋
最直接最不講道理的方式就是看源碼,使用之前一定要學(xué)會看說明書。
打算使用jupyter演示的,但是jupyter不支持看源碼,所以使用Pycharm吧。
from selenium import webdriver Broswer = webdriver.Chrome(executable_path="chromedriver", port=0, ? ? ? ? ? ? ? ? ?options=None, service_args=None, ? ? ? ? ? ? ? ? ?desired_capabilities=None, service_log_path=None, ? ? ? ? ? ? ? ? ?chrome_options=None, keep_alive=True) print(webdriver.Chrome.__doc__) """ ?Controls the ChromeDriver and allows you to drive the browser. ?You will need to download the ChromeDriver executable from ? ? http://chromedriver.storage.googleapis.com/index.html """ print(webdriver.Chrome.__init__.__doc__) """ Creates a new instance of the chrome driver. ? ? ? ? Starts the service and then creates new instance of chrome driver. ? ? ? ? :Args: ? ? ? ? ?- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH ? ? ? ? ?- port - port you would like the service to run, if left as 0, a free port will be found. ? ? ? ? ?- options - this takes an instance of ChromeOptions ? ? ? ? ?- service_args - List of args to pass to the driver service ? ? ? ? ?- desired_capabilities - Dictionary object with non-browser specific ? ? ? ? ? ?capabilities only, such as "proxy" or "loggingPref". ? ? ? ? ?- service_log_path - Where to log information from the driver. ? ? ? ? ?- chrome_options - Deprecated argument for options ? ? ? ? ?- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive. """
Chrome Doc解釋: 控制ChromeDriver并允許您驅(qū)動瀏覽器。
你需要從http://chromedriver.storage.googleapis.com/index.html下載ChromeDriver的可執(zhí)行文件。
沒有下ChromeDriver的小伙伴根據(jù)自己瀏覽器版本下載相應(yīng)的ChromeDriver版本,兩者版本關(guān)系ChromeDriver版本>=Chrome版本。
參數(shù)解釋:
executable_path- 可執(zhí)行文件的路徑。如果使用默認(rèn)值,則假定可執(zhí)行文件位于$PATH中。port- 你希望服務(wù)運行的端口,如果為0,使用空閑端口。options- 這是ChromeOptions的一個實例service_args- 要傳遞給驅(qū)動程序服務(wù)的args列表desired_capabilities-僅具有非瀏覽器特定功能的字典對象,例如“proxy”或“loggingPref”。service_log_path- 記錄來自驅(qū)動程序的信息存放路徑。chrome_options- chrome選項。keep_alive-是否配置ChromeRemoteConnection使用HTTP keep-alive。
其中options和chrome_options:使用options代替chrome_options。
?if chrome_options:
? ? ? ? ? ? warnings.warn('use options instead of chrome_options',
? ? ? ? ? ? ? ? ? ? ? ? ? DeprecationWarning, stacklevel=2)
? ? ? ? ? ? options = chrome_options并且告知此警告被棄用
關(guān)于executable_path解釋:value是ChromeDriver.exe路徑。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決django框架model中外鍵不落實到數(shù)據(jù)庫問題
這篇文章主要介紹了解決django框架model中外鍵不落實到數(shù)據(jù)庫問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python?matplotlib實現(xiàn)多子圖布局
多子圖布局是指在一個圖像中同時顯示多個子圖,每個子圖可以是獨立的圖形或者是相互關(guān)聯(lián)的圖形,下面我們就來了解下matplotlib是如何實現(xiàn)多子圖布局的吧2023-12-12
python用pd.read_csv()方法來讀取csv文件的實現(xiàn)
本文主要介紹了python用pd.read_csv()方法來讀取csv文件的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
YOLOv5以txt或json格式輸出預(yù)測結(jié)果的方法詳解
這篇文章主要給大家介紹了關(guān)于YOLOv5以txt或json格式輸出預(yù)測結(jié)果的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-03-03

