|
背景知識:
phantomjs是一個基於webkit的沒有介面的瀏覽器,所以運行起來比完整的瀏覽器要高效。
selenium是一個測試web應用的工具,目前是2.42.1版本,和1版的區別在於2.0+中把WebDrive整合在了一起。
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:
#coding=utf-8from selenium import webdriverdriver = webdriver.PhantomJS(executable_path=‘C:\Users\Gentlyguitar\Desktop\phantomjs-1.9.7-windows\phantomjs.exe‘)driver.get("http://duckduckgo.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()
其中的executable_path就是剛才phantomjs.exe的路徑,運行結果:
https://duckduckgo.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:\Users\Gentlyguitar\Desktop\phantomjs-1.9.7-windows\phantomjs.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_urlprint driver.page_source
列印網頁的兩個屬性:url和source
參考文獻:
http://www.realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/#.U5FXUvmSziE
http://selenium-python.readthedocs.org/getting-started.html
http://www.ruanyifeng.com/blog/2009/07/xpath_path_expressions.html
http://www.cnblogs.com/paisen/p/3310067.html |