[Python爬蟲] Windows下Selenium自動訪問Firefox和Chrome並實現搜尋截圖

來源:互聯網
上載者:User

標籤:python   selenium   基礎知識   firefox   自動搜尋   

        前兩篇文章介紹了安裝,此篇文章算是一個簡單的進階應用吧!它是在Windows下通過Selenium+Python實現自動訪問Firefox和Chrome並實現搜尋的功能。
        [Python爬蟲] 在Windows下安裝PhantomJS和CasperJS及入門介紹(上)
        [Python爬蟲] 在Windows下安裝PIP+Phantomjs+Selenium

自動訪問Firefox

        可以參照前文安裝Selenium環境,目前Selenium這個用於Web應用程式測試的工具支援的瀏覽器包括IE、Mozilla Firefox、Mozilla Suite、Chrome等。但是由於Firefox是預設安裝路徑,webdriver可以正常訪問它,而Chrome和IE需要設定driver路徑。

from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport sys reload(sys) sys.setdefaultencoding('gb18030') driver = webdriver.Firefox()driver.get("http://www.baidu.com")assert "百度" in driver.titleelem = driver.find_element_by_name("wd")elem.send_keys("Eastmount")elem.send_keys(Keys.RETURN)assert "Google" in driver.titledriver.save_screenshot('baidu.png')driver.close()driver.quit()
        運行效果如所示,自動調用Firefox瀏覽器搜尋,同時輸出斷言錯誤:
        assert "Google" in driver.title AssertionError


源碼分析
        官方文檔地址:http://selenium-python.readthedocs.org/getting-started.html

from selenium import webdriver
       from selenium.webdriver.common.keys import Keys
       import sys

        首先匯入Selenium.webdriver模板,它提供了webdriver的實現方法,目前支援這些方法的有Firefox、Chrome、IE和Remote。同時匯入Keys類,它提供了操作鍵盤的快速鍵,如RETURE、F1、ALT等。最後匯入sys主要是設定編碼方式。

reload(sys)
       sys.setdefaultencoding(‘gb18030‘) 

        由於漢語中可能會遇到錯誤:
        UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xc4 in position 33
        UnicodeDecodeError: ‘utf8‘ codec can‘t decode byte 0xb0 in position 35
        所以此處轉換成gb編碼,該篇不重點介紹了。

driver = webdriver.Firefox()
       driver.get("http://www.baidu.com")

        建立Firefoxwebdriver執行個體。其中Firefox最簡單,其他Chrome還需要driver和配置路徑。接下來通過driver.get()開啟百度URL網頁,webdriver會等待網頁元素載入完成之後才把控制權交回指令碼。但是,如果要開啟了頁面在載入的過程中包含了很多AJAX,webdriver可能無法準確判斷頁面何時載入完成。

assert "百度" in driver.title
       assert "Google" in driver.title

         接下來使用斷言判斷文章的標題Title是否包含“百度”和“Google”。對應的標題是“百度一下,你就知道”,所以其中“百度”包括,而“Google”會出現斷言報錯。
        同時提交頁面並獲得返回結果,為了判斷結果是否成功返回也可以使用斷言。

elem = driver.find_element_by_name("wd")

        webdriver提供了很多如find_element_by_*的方法來匹配要尋找的元素。如利用name屬性尋找方法find_element_by_name來定位輸入框,審查元素name=wd。
        元素定位方法可以參考官網:Locating Elements


elem.send_keys("Eastmount")

elem.send_keys(Keys.RETURN)

        send_keys方法可以用來類比鍵盤操作,相當於是在搜尋方塊中輸入“Eastmount”再按斷行符號鍵搜尋。但首先要從selenium.webdriver.common.keys匯入Keys類。

driver.save_screenshot(‘baidu.png‘)

driver.close()

driver.quit()

        最後是調用save_screenshot進行,但是圖片是過程中的,怎樣擷取最後載入的圖片呢?同時,操作完成並關閉瀏覽器。當然,也可以調用quit()方法,兩者的區別在於:quit()方法會退出瀏覽器,而close()方法只是關閉頁面,但如果只有一個頁面被開啟,close()方法同樣會退出瀏覽器。


自動訪問Chrome
        首先下載chromedriver共置於Chrome安裝目錄。可能會遇到錯誤:
        WebDriverException: Message: ‘chromedriver‘ executable needs to be in PATH.參考官網解決方案:How to use chromedriver,我採用的是設定driver環境。
        代碼如下:
import osfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keyschromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"os.environ["webdriver.chrome.driver"] = chromedriverdriver = webdriver.Chrome(chromedriver)driver.get("http://www.python.org")assert "Python" in driver.titleelem = driver.find_element_by_name("q")elem.send_keys("selenium")elem.send_keys(Keys.RETURN)assert "Google" in driver.titledriver.close()driver.quit()
        需要放置chromedriver如下路徑,同時可以通過代碼設定。但是由於我的Chrome可能Bug一直未修複,總是開啟錯誤。

driver = webdriver.Chrome(executable_path="G:\chromedriver.exe")



參考資料:        用python玩轉selenium:2-入門執行個體及分析 - Reiki
        構建Python+Selenium2自動化測試環境<二>:IE、Chrome和Firefox運行
        用selenium實現某微博搜尋資料的抓取
        RobotFramework+seleniumlibrary Web自動化測試 (三)

         最後希望該篇基礎性文章對你有所協助吧!如果有不足之處,還請海涵~
      (By:Eastmount 2015-8-20 下午4點   http://blog.csdn.net/eastmount/)


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

[Python爬蟲] Windows下Selenium自動訪問Firefox和Chrome並實現搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.