• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 298  文章 - 377  trackbacks - 0
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(34)

            隨筆分類

            隨筆檔案

            文章檔案

            相冊

            收藏夾

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            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)加載一個手機端的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)  #設(shè)置userAgent
            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) #加載網(wǎng)址
            obj.get('http://wap.95533pc.com')#打開網(wǎng)址
            obj.save_screenshot("1.png")   #截圖保存
            obj.quit()  # 關(guān)閉瀏覽器。當(dāng)出現(xiàn)異常時記得在任務(wù)瀏覽器中關(guān)閉PhantomJS,因為會有多個PhantomJS在運行狀態(tài),影響電腦性能

            一、超時設(shè)置

            webdriver類中有三個和時間相關(guān)的方法:

            1.pageLoadTimeout    設(shè)置頁面完全加載的超時時間,完全加載即完全渲染完成,同步和異步腳本都執(zhí)行完

            2.setScriptTimeout    設(shè)置異步腳本的超時時間

            3.implicitlyWait         識別對象的智能等待時間

            下面我們以獲取校花網(wǎng)title為例來驗證效果,因為校花網(wǎng)中圖片比較多,所以加載的時間比較長,更能時間我們的效果(另一原因我就不說了,這樣才能讓我們學(xué)起來帶勁,哈哈!!!)

            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)簽,然后就可以進行后面的操作了

            更多具體關(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')                    #通過ID定位
                obj.find_element_by_class_name('s_ipt')         #通過class屬性定位
                obj.find_element_by_name('wd')                  #通過標(biāo)簽name屬性定位
                obj.find_element_by_tag_name('input')           #通過標(biāo)簽屬性定位
                obj.find_element_by_css_selector('#kw')         #通過css方式定位
                obj.find_element_by_xpath("http://input[@id='kw']"#通過xpath方式定位
                obj.find_element_by_link_text("貼吧")           #通過xpath方式定位
             
                print obj.find_element_by_id('kw').tag_name   #獲取標(biāo)簽的類型
            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()  #設(shè)置全屏
            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') #設(shè)置瀏覽器寬480,高800
            try:
                obj.get('http://www.baidu.com')
                obj.save_screenshot('12.png'# 截取全屏,并保存
            except Exception as e:
                print e

            3、操作瀏覽器前進、后退

            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)的對象進行某些操作,以達到我們某些特定的目的,那我們下面就介紹下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()              #用于清除輸入框的內(nèi)容
                obj.find_element_by_id('kw').send_keys('Hello'#在輸入框內(nèi)輸入Hello
                obj.find_element_by_id('su').click()              #用于點擊按鈕
                obj.find_element_by_id('su').submit()             #用于提交表單內(nèi)容
             
            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)   #用于清除輸入框的內(nèi)容,相當(dāng)于clear()
                obj.find_element_by_id('kw').send_keys('Hello')   #在輸入框內(nèi)輸入Hello
                obj.find_element_by_id('su').send_keys(Keys.ENTER) #通過定位按鈕,通過enter(回車)代替click()
             
            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)   #用于清除輸入框的內(nèi)容,相當(dāng)于clear()
                obj.find_element_by_id('kw').send_keys('Hello')   #在輸入框內(nèi)輸入Hello
                obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'a')   #ctrl + a 全選輸入框內(nèi)容
                obj.find_element_by_id('kw').send_keys(Keys.CONTROL,'x')   #ctrl + x 剪切輸入框內(nèi)容
             
            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()                       #提交表單內(nèi)容
                f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....')             #定位到要點擊的標(biāo)簽
                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()                       #提交表單內(nèi)容
                f = obj.find_element_by_xpath('/html/body/div/div[2]/div[2]/....')             #定位到要點擊的標(biāo)簽
                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 聶文龍 閱讀(295) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            人妻无码久久精品| 国产精品欧美久久久久天天影视| 国产高清美女一级a毛片久久w| 久久久久人妻精品一区| 99久久国产热无码精品免费| 7777久久久国产精品消防器材| 久久久久亚洲AV成人网人人网站| 99久久夜色精品国产网站| 久久婷婷人人澡人人爽人人爱| 三级三级久久三级久久| 色欲久久久天天天综合网精品| 熟妇人妻久久中文字幕| 狠狠色丁香婷婷久久综合不卡| 四虎国产精品免费久久5151| 国产农村妇女毛片精品久久| 久久国产精品二国产精品| 亚洲伊人久久综合影院| 久久久久亚洲AV无码观看| 久久久久久久人妻无码中文字幕爆| 久久久久综合网久久| 久久久WWW成人免费精品| 久久久久99这里有精品10| 欧洲人妻丰满av无码久久不卡| 精品久久一区二区| 性做久久久久久久久| 久久国产欧美日韩精品| 精品久久久久一区二区三区| 久久频这里精品99香蕉久| 久久久久四虎国产精品| 亚洲AV伊人久久青青草原| 久久青青草原精品国产| 亚洲v国产v天堂a无码久久| 久久精品人人做人人妻人人玩| 久久久久黑人强伦姧人妻| 国产精品久久久久久吹潮| 婷婷久久综合九色综合九七| 欧美va久久久噜噜噜久久| 一级做a爰片久久毛片毛片| 2021久久精品国产99国产精品| 国产精品一区二区久久精品涩爱| 国产精品久久久久影院嫩草|