selenium+phantomjs解析JS


背景知識:

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-8
from selenium import webdriver

driver = 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_url
driver.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-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
import sys

driver = 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_url
print driver.page_source
driver.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM