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)文章
Python 轉(zhuǎn)換數(shù)據(jù)類型函數(shù)和轉(zhuǎn)換數(shù)據(jù)類型的作用
這篇文章主要介紹了Python 轉(zhuǎn)換數(shù)據(jù)類型函數(shù)和轉(zhuǎn)換數(shù)據(jù)類型的作用,圍繞Python 轉(zhuǎn)換數(shù)據(jù)類型的相關(guān)資料展開內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下2022-03-03Python基礎(chǔ)教程之增加和去除數(shù)字的千位分隔符
千位分隔符其實就是數(shù)字中的逗號,下面這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)教程之增加和去除數(shù)字的千位分隔符,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01如何利用Anaconda配置簡單的Python環(huán)境
這篇文章主要為大家詳細(xì)介紹了如何利用Anaconda配置簡單的Python環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06keras 自定義loss model.add_loss的使用詳解
這篇文章主要介紹了keras 自定義loss model.add_loss的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python+pygame實現(xiàn)坦克大戰(zhàn)小游戲的示例代碼(可以自定義子彈速度)
這篇文章主要介紹了python+pygame實現(xiàn)坦克大戰(zhàn)小游戲---可以自定義子彈速度,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08python使用numpy實現(xiàn)直方圖反向投影示例
今天小編就為大家分享一篇python使用numpy實現(xiàn)直方圖反向投影示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01