Selenium與PhantomJS入門

Selenium 是一個Web的自動化測試工具,最初是為網站自動化測試而開發的。類型像我們玩遊戲用的按鍵精靈,可以按指定的命令自動化操作,不同是Selenium可以直接運行在瀏覽器上,它支持所有主流的瀏覽器(包括PhantomJS這些無界面的瀏覽器)。

Selenium可以根據我們的指令,讓瀏覽器自動加載頁面,獲取需要的頁面,甚至頁面截屏,或者判斷網站某些動作是否發生。

Selenium自己不帶瀏覽器,不支持瀏覽器的功能。它需要與第三方瀏覽器結合在一起才能使用。但是我們有時候需要讓它內嵌在代碼中運行,所以我們使用一個加PhantomJS的工具代替真實的瀏覽器。

可以從PyPI網站下載Selenium庫pypi.python.org/simple/sule…,也可以用第三方管理器pip命令安裝:pip install seleniumSelenium官方參考文檔:selenium-python.readthedocs.io/index.html

PhantomJS

PhantomJS 是一個基於Webkit的“無界面”(headless)L瀏覽器,他會把網站加載到內存並執行頁面上的JavaScript,因為不會展示圖形界面,所以運行起來比完整的瀏覽器更高效。

如果我們把Selenium和PhantomJS結合在一起,就可以運行一個非常強大的網絡爬醜,這個爬蟲可以處理JavaScript、Cookie、headers,以及任何我們真實用戶需要做的事情。

Python爬蟲_Selenium與PhantomJS入門

快速入門

Selenium庫裡有一個叫WebDriver的API。WebDriver 可以控制瀏覽器的操作,它可以想BeautifulSoup或者其他的Selector對象一樣用來查找頁面的元素,與頁面上的元素進行交互(發送文本、點擊等),以及執行其他動作來運行網絡爬蟲。

Python爬蟲_Selenium與PhantomJS入門

Python爬蟲_Selenium與PhantomJS入門

頁面操作

Selenium的WebDriver提供了各種方法來尋找元素,假設下面有一個表單輸入框:

Python爬蟲_Selenium與PhantomJS入門

Python爬蟲_Selenium與PhantomJS入門

定位UI元素(WebElements)

關於元素的選取,有如下的API單個元素選取

Python爬蟲_Selenium與PhantomJS入門

1.By ID

Python爬蟲_Selenium與PhantomJS入門

實現

Python爬蟲_Selenium與PhantomJS入門

---------or-------

Python爬蟲_Selenium與PhantomJS入門

2.By Class Name

Python爬蟲_Selenium與PhantomJS入門

實現

Python爬蟲_Selenium與PhantomJS入門

----------------or--------------------

Python爬蟲_Selenium與PhantomJS入門

3.By Tag Name

Python爬蟲_Selenium與PhantomJS入門

實現

Python爬蟲_Selenium與PhantomJS入門

4.By Name

Python爬蟲_Selenium與PhantomJS入門

實現

Python爬蟲_Selenium與PhantomJS入門

5.By Link Text

Python爬蟲_Selenium與PhantomJS入門

實現

Python爬蟲_Selenium與PhantomJS入門

6.By Partial Link Text

Python爬蟲_Selenium與PhantomJS入門

實現

Python爬蟲_Selenium與PhantomJS入門

7.By CSS

Python爬蟲_Selenium與PhantomJS入門

Python爬蟲_Selenium與PhantomJS入門

8.By XPath

Python爬蟲_Selenium與PhantomJS入門

實現

Python爬蟲_Selenium與PhantomJS入門

鼠標動作鏈

有些時候,我們需要在頁面上模擬一些鼠標操作,比如雙擊、右擊、拖拽甚至按住不同等,我們可以通過導入ActionChains類來做到。

示例

Python爬蟲_Selenium與PhantomJS入門

填充表單

我們已經知道怎樣想文本框中輸入文字,但是有時候我們會碰到 標籤的下拉框。直接點擊下拉框中的選項不一定可以行。

Python爬蟲_Selenium與PhantomJS入門

Selenium專門提供了Select類處理下拉框。其實WebDriver中提供了一個叫Select的方法,可以幫助我們完成這些事情:

Python爬蟲_Selenium與PhantomJS入門

以上是三種選擇下拉框的方式,它可以根據索引來選擇,可以根據值來選擇,可以根據文字來選擇。注意:

Python爬蟲_Selenium與PhantomJS入門

全部取消怎麼辦?

Python爬蟲_Selenium與PhantomJS入門

彈窗處理

當你觸發了某個時間之後,頁面出現了彈窗提示,處理這個提示或者獲取提示信息方法如下:

Python爬蟲_Selenium與PhantomJS入門

頁面切換

一個瀏覽器肯定會有很多窗口,所以我們肯定要有方法來實現窗口的切換,切換窗口的方法如下:

Python爬蟲_Selenium與PhantomJS入門

也可以使用window_handles方法來獲取每個窗口的操作對象。例如:

Python爬蟲_Selenium與PhantomJS入門

頁面的前進和後退

操作頁面的前進和後退功能:

Python爬蟲_Selenium與PhantomJS入門

Cookies

獲取頁面每個Cookies值,用法如下:

Python爬蟲_Selenium與PhantomJS入門

刪除Cookies,用法如下:

Python爬蟲_Selenium與PhantomJS入門

頁面等待

注意:這是非常重要的一部分!

Python爬蟲_Selenium與PhantomJS入門

顯式等待

顯示等待指定了某個條件,然後設置最長等待事件。如果在這個時間還找到沒有元素,那麼便會拋出異常。

Python爬蟲_Selenium與PhantomJS入門

如果不寫參數,程序默認會0.5s調用一次來來查看安蘇是否已經生成,如果本來元素時存在的,那麼會立即返回。

下面是一些內置的等待條件,你可以直接調用這些條件,而不用自己寫某些等待條件了。

Python爬蟲_Selenium與PhantomJS入門

隱式等待

隱式等待比較簡單,就是簡單地設置一個等待時間,單位為秒。

Python爬蟲_Selenium與PhantomJS入門

如果不設置,默認等待時間為0。

Python爬蟲_Selenium與PhantomJS入門

私信+資料


分享到:


相關文章: