欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python webdriver.Chrome()的使用解讀

 更新時間:2023年02月25日 09:20:17   作者:HHT0506  
這篇文章主要介紹了Python webdriver.Chrome()的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

webdriver.Chrome()的使用

1.前提

Python與Chrome路徑下均安裝chromedriver.exe。

2.chromedriver.exe版本選擇及下載

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)文章

最新評論