溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python?webdriver.Chrome()如何使用

發(fā)布時(shí)間:2023-02-25 10:38:21 來(lái)源:億速云 閱讀:140 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Python webdriver.Chrome()如何使用”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Python webdriver.Chrome()如何使用”文章能幫助大家解決問(wèn)題。

    webdriver.Chrome()的使用

    1.前提

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

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

    • Chrome版本查看:瀏覽器右上角三個(gè)點(diǎn)->幫助->關(guān)于Google Chrome

    Python?webdriver.Chrome()如何使用

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

    Python?webdriver.Chrome()如何使用

    3.安裝

    下載后解壓,將 chromedriver.exe復(fù)制到下面兩個(gè)目錄中:

    • Chrome目錄:比如C:\Program Files (x86)\Google\Chrome\Application

    • Python目錄:比如D:\Softwares\Python39

    4.添加環(huán)境變量

    將上述Chrome路徑添加進(jìn)系統(tǒng)環(huán)境光變量,Python使用時(shí)應(yīng)該加入環(huán)境變量了,這個(gè)就不用管了。

    5.測(cè)試代碼

    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ù)解釋

    通過(guò)源碼看解釋

    最直接最不講道理的方式就是看源碼,使用之前一定要學(xué)會(huì)看說(shuō)明書。

    打算使用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ū)動(dòng)瀏覽器。

    你需要從http://chromedriver.storage.googleapis.com/index.html下載ChromeDriver的可執(zhí)行文件。

    沒(méi)有下ChromeDriver的小伙伴根據(jù)自己瀏覽器版本下載相應(yīng)的ChromeDriver版本,兩者版本關(guān)系ChromeDriver版本>=Chrome版本。

    參數(shù)解釋:

    • executable_path - 可執(zhí)行文件的路徑。如果使用默認(rèn)值,則假定可執(zhí)行文件位于$PATH中。

    • port- 你希望服務(wù)運(yùn)行的端口,如果為0,使用空閑端口。

    • options - 這是ChromeOptions的一個(gè)實(shí)例

    • service_args - 要傳遞給驅(qū)動(dòng)程序服務(wù)的args列表

    • desired_capabilities -僅具有非瀏覽器特定功能的字典對(duì)象,例如“proxy”或“l(fā)oggingPref”。

    • service_log_path - 記錄來(lái)自驅(qū)動(dòng)程序的信息存放路徑。

    • chrome_options - chrome選項(xiàng)。

    • 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路徑。

    關(guān)于“Python webdriver.Chrome()如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI