selenium+Python學習之一

來源:互聯網
上載者:User

標籤:one   尋找   補充   highlight   關於   path   path環境變數   nbsp   str   

剛入門selenium+Python,實驗成功之後,記錄一下過程。

首先是在知乎上面看到一個關於selenium+python的樣本,於是自己便嘗試搭建環境上手實驗。

按照作者的代碼敲一遍之後執行,竟然報錯了~

代碼如下:

# coding=utf-8from selenium import webdriverbrowser = webdriver.Firefox()browser.get("http://zhihu.com")browser.close()  

然後運行,竟然報錯了。

其實上面代碼沒有問題,但是,我想通過API來熟悉一下webdriver,於是在WebDriver API上面查看:http://selenium-python.readthedocs.io/api.html

按照API樣本編寫代碼如下:

# coding=utf-8from selenium.webdriver import Firefoxfrom selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListenerclass MyListener(AbstractEventListener):    def before_navigate_to(self, url, driver):        print("Before navigate to %s" % url)    def after_navigate_to(self, url, driver):        print("After navigate to %s" % url)driver = Firefox()ef_driver=EventFiringWebDriver(driver,MyListener())ef_driver.get("http://zhihu.com")assert "TestArt" in ef_driver.titleef_driver.close()

執行之後,錯誤如下:selenium.common.exceptions.WebDriverException: Message: ‘geckodriver‘ executable needs to be in PATH.

尋找到一篇已經解決改問題的文章:http://www.cnblogs.com/yousuosiyss/p/6882420.html

摘選其中一種方式【下載geckodriver.exe】,步驟如下:

1.:https://github.com/mozilla/geckodriver/releases,根據自己的電腦,下載的win64位的;
2.在firefox的安裝目錄下,解壓geckodriver,然後將該路徑添加到path環境變數下,不報這個錯了;
3.但是,報了一個新的錯“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”;
4.繼續網上查,原因是,我下載的geckodriver是V0.16.1版本的,這個版本和selenium3.4.2不相容,需要使用deckdriverV0.15的版本;
5.重新下載了deckodriverV0.15的版本,ok了,不報這個錯了,但是,又報了一個新的錯“selenium.common.exceptions.WebDriverException: Message: Unsupported Marionette protocol version 2, required 3;
6.繼續往上查原因,說是firefox版本太低了,需要升級到最新版,ok,更新firefox,問題解決。

其實按照作者的分享,很快便能解決該問題,但是,我按照作者的方式正常操作下來,仍然報一開始的錯,各種嘗試未果,然後在網上搜尋他人的經驗分享,才終於解決了這個問題。
以下則是嘗試未果的補充:
步驟2,是將firefox的安裝目錄添加到path環境變數中,添加完成之後,如果仍報一樣的錯誤,重啟cmd與IDE,一般就正常了,如果還有作者所述的錯誤,可以繼續按照作者的上述步驟操作。

以上即為小小心得,記錄一下。

 

selenium+Python學習之一

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.