Selenium
一、簡介
selenium是一個用于Web應(yīng)用自動化程序測試的工具,測試直接運行在瀏覽器中,就像真正的用戶在操作一樣
selenium2支持通過驅(qū)動真實瀏覽器(FirfoxDriver,IternetExplorerDriver,OperaDriver,ChromeDriver)
selenium2支持通過驅(qū)動無界面瀏覽器(HtmlUnit,PhantomJs)
二、安裝
Windows
第一種方法是:下載源碼安裝,下載地址(https://pypi.python.org/pypi/selenium)解壓并把整個目錄放到C:\Python27\Lib\site-packages下面
第二種方法是:可以直接在C:\Python27\Scripts 下輸入命令安裝 pip install -U selenium
Linux
|
1
|
sudo pip install selenium
|
PhantomJS
一、簡介
PhantomJS 是一個基于 WebKit(WebKit是一個開源的瀏覽器引擎,Chrome,Safari就是用的這個瀏覽器引擎) 的服務(wù)器端 JavaScript API,
主要應(yīng)用場景是:無需瀏覽器的 Web 測試,頁面訪問自動化,屏幕捕獲,網(wǎng)絡(luò)監(jiān)控
二、安裝
Windows
下載源碼安裝,下載地址(http://phantomjs.org/download.html)解壓并把解壓縮的路徑添加到環(huán)境變量中即可,我自己的放到了C:\Python27\Scripts 下面
Linux
|
1
|
sudo apt-get install PhantomJS
|
Selenium + PhantomJS + python 簡單實現(xiàn)爬蟲的功能
python可以使用selenium執(zhí)行javascript,selenium可以讓瀏覽器自動加載頁面,獲取需要的數(shù)據(jù)。selenium自己不帶瀏覽器,可以使用第三方瀏覽器如Firefox,Chrome等,也可以使用headless瀏覽器如PhantomJS在后臺執(zhí)行。
在工作用遇到一個問題,當(dāng)加載一個手機(jī)端的URL時候,會加載不上,需要我們在請求頭中設(shè)置一個User-Agent,設(shè)置完以后就可以打開了(Windows下執(zhí)行,linux下執(zhí)行的話就不用加executable_path='C:\Python27\Scripts\phantomjs.exe')
|
1
2
3
4
5
6
7
8
9
10
|
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 ")
obj = webdriver.PhantomJS(executable_path='C:\Python27\Scripts\phantomjs.exe',desired_capabilities=dcap)
obj.get('http://wap.95533pc.com')
obj.save_screenshot("1.png")
obj.quit()
|
一、超時設(shè)置
webdriver類中有三個和時間相關(guān)的方法:
1.pageLoadTimeout 設(shè)置頁面完全加載的超時時間,完全加載即完全渲染完成,同步和異步腳本都執(zhí)行完
2.setScriptTimeout 設(shè)置異步腳本的超時時間
3.implicitlyWait 識別對象的智能等待時間
下面我們以獲取?;ňW(wǎng)title為例來驗證效果,因為校花網(wǎng)中圖片比較多,所以加載的時間比較長,更能時間我們的效果(另一原因我就不說了,這樣才能讓我們學(xué)起來帶勁,哈哈?。。。?/p>
|
1
2
3
4
5
6
7
8
|
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.xiaohuar.com')
print obj.title
except Exception as e:
print e
|
二、元素的定位
對象的定位是通過屬性定位來實現(xiàn)的,這種屬性就像人的身份證信息一樣,或是其他的一些信息來找到這個對象,那我們下面就介紹下Webdriver提供的幾個常用的定位方法
|
1
|
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
|
上面這個是百度的輸入框,我們可以發(fā)現(xiàn)我們可以用id來定位這個標(biāo)簽,然后就可以進(jìn)行后面的操作了
更多具體關(guān)于XPath的信息,請具體參照 http://www.cnblogs.com/hyddd/archive/2009/05/22/1487332.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
obj.find_element_by_id('kw')
obj.find_element_by_class_name('s_ipt')
obj.find_element_by_name('wd')
obj.find_element_by_tag_name('input')
obj.find_element_by_css_selector('#kw') #通過css方式定位
obj.find_element_by_xpath("http://input[@id='kw']")
obj.find_element_by_link_text("貼吧")
print obj.find_element_by_id('kw').tag_name
except Exception as e:
print e
|
三、瀏覽器的操作
1、調(diào)用啟動的瀏覽器不是全屏的,有時候會影響我們的某些操作,所以我們可以設(shè)置全屏
|
1
2
3
4
5
6
7
8
9
|
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
obj.maximize_window()
try:
obj.get('http://www.baidu.com')
obj.save_screenshot('11.png')
except Exception as e:
print e
|
2、設(shè)置瀏覽器寬、高
|
1
2
3
4
5
6
7
8
9
|
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
obj.set_window_size('480','800')
try:
obj.get('http://www.baidu.com')
obj.save_screenshot('12.png')
except Exception as e:
print e
|
3、操作瀏覽器前進(jìn)、后退
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
try:
obj.get('http://www.baidu.com')
obj.save_screenshot('1.png')
obj.get('http://www.sina.com.cn')
obj.save_screenshot('2.png')
obj.back()
obj.save_screenshot('3.png')
obj.forward()
obj.save_screenshot('4.png')
except Exception as e:
print e
|
四、操作測試對象
定位到元素以后,我們就應(yīng)該對相應(yīng)的對象進(jìn)行某些操作,以達(dá)到我們某些特定的目的,那我們下面就介紹下Webdriver提供的幾個常用的操作方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from selenium import webdriver
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
print obj.find_element_by_id("cp").text
obj.find_element_by_id('kw').clear()
obj.find_element_by_id('kw').send_keys('Hello')
obj.find_element_by_id('su').click()
obj.find_element_by_id('su').submit()
except Exception as e:
print e
|
五、鍵盤事件
1、鍵盤按鍵用法
|
1
2
3
4
5
6
7
8
9
10
11
|
from selenium.webdriver.common.keys import Keys
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
obj.find_element_by_id('kw').send_keys(Keys.TAB)
obj.find_element_by_id('kw').send_keys('Hello')
obj.find_element_by_id('su').send_keys(Keys.ENTER)
except Exception as e:
print e
|
2、鍵盤組合鍵使用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
obj.set_page_load_timeout(5)
try:
obj.get('http://www.baidu.com')
obj.find_element_by_id('kw').send_keys(Keys.TAB)
obj.find_element_by_id('kw').send_keys('Hello')
obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'a')
obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'x')
except Exception as e:
print e
|
六、中文亂碼問題
selenium2 在python的send_keys()中輸入中文會報錯,其實在中文前面加一個u變成unicode就能搞定了
七、鼠標(biāo)事件
1、鼠標(biāo)右擊
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
try:
obj.get("http://pan.baidu.com")
obj.find_element_by_id('TANGRAM__PSP_4__userName').send_keys('13201392325')
obj.find_element_by_id('TANGRAM__PSP_4__password').send_keys('18399565576lu')
obj.find_element_by_id('TANGRAM__PSP_4__submit').submit()
f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....')
ActionChains(obj).context_click(f).perform()
except Exception as e:
print e
|
2、鼠標(biāo)雙擊
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
obj = webdriver.PhantomJS(executable_path="D:\Python27\Scripts\phantomjs.exe")
try:
obj.get("http://pan.baidu.com")
obj.find_element_by_id('TANGRAM__PSP_4__userName').send_keys('13201392325')
obj.find_element_by_id('TANGRAM__PSP_4__password').send_keys('18399565576lu')
obj.find_element_by_id('TANGRAM__PSP_4__submit').submit()
f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....')
ActionChains(obj).double_click(f).perform()
except Exception as e:
print e
|
八、cookie處理
@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2017-12-01 11:26
聶文龍 閱讀(301)
評論(0) 編輯 收藏 引用