selenium+phantomjs解析JS

來源:互聯網
上載者:User

標籤:ajax   target   sys   網路   移動   滑鼠   ons   action   top   

背景知識:

PhantomJS 是一個基於WebKit的伺服器端 JavaScript API。它全面支援web而不需瀏覽器支援,其快速,原生支援各種Web標準: DOM 處理, CSS 選取器, JSON, Canvas, 和 SVG。PhantomJS可以用於頁面自動化,網路監測,網頁截屏,以及無介面測試等。

Selenium也是一個用於Web應用程式測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的使用者在操作一樣。支援的瀏覽器包括IE(7、8、9)、Mozilla Firefox、Mozilla Suite等。這個工具的主要功能包括:測試與瀏覽器的相容性——測試你的應用程式看是否能夠很好得工作在不同瀏覽器和作業系統之上。

PhantomJS 用來渲染解析JS,Selenium 用來驅動以及與 Pyt

#coding=utf-8from selenium import webdriverdriver = webdriver.PhantomJS(executable_path=‘C:UsersGentlyguitarDesktopphantomjs-1.9.7-windowsphantomjs.exe‘)driver.get("http://phperz.com/")driver.find_element_by_id(‘search_form_input_homepage‘).send_keys("Nirvana")driver.find_element_by_id("search_button_homepage").click()print driver.current_urldriver.quit()

 

hon 的對接,Python 進行後期的處理。

selenium2支援的Python版本:2.7, 3.2, 3.3 and 3.4

如果需要進行遠程操作的話,就需要額外安裝selenium server

安裝:

先裝selenium2,哪種方式裝都可以,我一般都是直接下載壓縮包,然後用python setup.py install命令來裝,selenium 2.42.1的:https://pypi.python.org/pypi/selenium/2.42.1

然後下載phantomjs,https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-windows.zip,解壓後可以看到一個phantomjs.exe的檔案

範例1

 

其中的executable_path就是剛才phantomjs.exe的路徑,運行結果:

https://phperz.com/?q=Nirvana

Walk through of the example

 值得一提的是:

get方法會一直等到頁面被完全載入,然後才會繼續程式

但 是對於ajax: It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded

send_keys就是填充input

範例2

 

#coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver import ActionChainsimport timeimport sysdriver = webdriver.PhantomJS(executable_path=‘C:UsersGentlyguitarDesktopphantomjs-1.9.7-windowsphantomjs.exe‘)driver.get("http://www.zhihu.com/#signin")#driver.find_element_by_name(‘email‘).send_keys(‘your email‘)driver.find_element_by_xpath(‘//input[@name="password"]‘).send_keys(‘your password‘)#driver.find_element_by_xpath(‘//input[@name="password"]‘).send_keys(Keys.RETURN)time.sleep(2)driver.get_screenshot_as_file(‘show.png‘)#driver.find_element_by_xpath(‘//button[@class="sign-button"]‘).click()driver.find_element_by_xpath(‘//form[@class="zu-side-login-box"]‘).submit()try:    dr=WebDriverWait(driver,5)    dr.until(lambda the_driver:the_driver.find_element_by_xpath(‘//a[@class="zu-top-nav-userinfo "]‘).is_displayed())except:    print ‘登入失敗‘    sys.exit(0)driver.get_screenshot_as_file(‘show.png‘)#user=driver.find_element_by_class_name(‘zu-top-nav-userinfo ‘)#webdriver.ActionChains(driver).move_to_element(user).perform() #移動滑鼠到我的使用者名稱loadmore=driver.find_element_by_xpath(‘//a[@id="zh-load-more"]‘)actions = ActionChains(driver)actions.move_to_element(loadmore)actions.click(loadmore)actions.perform()time.sleep(2)driver.get_screenshot_as_file(‘show.png‘)print driver.current_urlprint driver.page_sourcedriver.quit()

 

這個程式完成的是,登陸知乎,然後能自動點擊頁面下方的“更多”,以載入更多的內容

Walk through of the example

from selenium.webdriver.common.keys import Keys,keys這個類就是鍵盤上的鍵,文中的send_keys(Keys.RETURN)就是按一個斷行符號

from selenium.webdriver.support.ui import WebDriverWait是為了後面一個等待的操作

from selenium.webdriver import ActionChains是匯入一個動作的類,這句話的寫法,我找了很久

find_element推薦使用Xpath的方法,非常方便

Xpath運算式寫法教程:http://www.ruanyifeng.com/blog/2009/07/xpath_path_expressions.html

值得注意的是,避免選擇value帶有空格的屬性,譬如class = "country name"這種,不然會報錯,大概compound class之類的錯

檢查使用者密碼是否輸入正確的方法就是在填入後截屏看看

想要截屏,這麼一句話就行:

driver.get_screenshot_as_file(‘show.png‘)

 

但是,這裡的截屏是不帶捲軸的,就是給你把整個頁面全部照下來

try:

    dr=WebDriverWait(driver,5)

    dr.until(lambda the_driver:the_driver.find_element_by_xpath(‘//a[@class="zu-top-nav-userinfo "]‘).is_displayed())

except:

    print ‘登入失敗‘

    sys.exit(0)

是用來通過檢查某個元素是否被載入來檢查是否登入成功,我認為當個黑盒子用就可以了。其中5的解釋:5秒內每隔500毫秒掃描1次頁面變化,直到指定的元素

對於表單的提交,即可以選擇登入按鈕然後使用click方法,也可以選擇表單然後使用submit方法,後者能應付沒有登入按鈕的情況,所以推薦使用submit()

對於一次點擊,既可以使用click(),也可以使用一連串的action來實現,如文中:

loadmore=driver.find_element_by_xpath(‘//a[@id="zh-load-more"]‘)

actions = ActionChains(driver)

actions.move_to_element(loadmore)

actions.click(loadmore)

actions.perform()

這5句話其實就相當於一句話,find element然後click,但是action的適用範圍更廣,譬如在這個例子中,要點擊的是一個a標籤對象,我不知道為什麼直接用click不行,不起作用

print driver.current_url

print driver.page_source

列印網頁的兩個屬性:url和source

 轉載http://www.phperz.com/article/15/0829/117337.html

selenium+phantomjs解析JS

聯繫我們

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