qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          輕松自動(dòng)化---selenium-webdriver(python) (九)

          本節(jié)重點(diǎn):

            上傳文件

            文件上傳操作也比較常見功能之一,上傳功能沒有用到新有方法或函數(shù),關(guān)鍵是思路。

            上傳過程一般要打開一個(gè)本地窗口,從窗口選擇本地文件添加。所以,一般會(huì)卡在如何操作本地窗口添加上傳文件。

            其實(shí),在selenium  webdriver 沒我們想的那么復(fù)雜;只要定位上傳按鈕,通send_keys添加本地文件路徑就可以了。絕對路徑和相對路徑都可以,關(guān)鍵是上傳的文件存在。下面通地例子演示。

            upload_file.html

          <html>
          <head>
          <meta http-equiv="content-type" content="text/html;charset=utf-8" />
          <title>upload_file</title>
          <script type="text/javascript" async=""
          src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
          "></script>
          <link href=http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css rel="stylesheet" />
          <script type="text/javascript">
          </script>
          </head>
          <body>
          <div class="row-fluid">
          <div class="span6 well">
          <h3>upload_file</h3>
          <input type="file" name="file" />
          </div>
          </div>
          </body>
          <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
          </html>

            upload.py

          #coding=utf-8
          from selenium import webdriver
          import os,time

          driver = webdriver.Firefox()

          #腳本要與upload_file.html同一目錄
          file_path =  'file:///' + os.path.abspath('upload_file.html')
          driver.get(file_path)

          #定位上傳按鈕,添加本地文件
          driver.find_element_by_name("file").send_keys('D:\\selenium_use_case\upload_file.txt')
          time.sleep(2)

          driver.quit()

           其它有些應(yīng)用不好找,所以就自己創(chuàng)建頁面,這樣雖然麻煩,但腳本代碼突出重點(diǎn)。

            這里找一139郵箱的實(shí)例,有帳號的同學(xué)可以測試一下~!

            (登陸基礎(chǔ)版的139郵箱,網(wǎng)盤模塊上傳文件。)

            139upload.py

          #coding=utf-8
          from selenium import webdriver
          import os,time

          driver = webdriver.Firefox()

          driver.get(http://m.mail.10086.cn)
          driver.implicitly_wait(30)

          #登陸
          driver.find_element_by_id("ur").send_keys("手機(jī)號")
          driver.find_element_by_id("pw").send_keys("密碼")
          driver.find_element_by_class_name("loading_btn").click()
          time.sleep(3)

          #進(jìn)入139網(wǎng)盤模塊
          driver.find_element_by_xpath("/html/body/div[3]/a[9]/span[2]").click()
          time.sleep(3)

          #上傳文件
          driver.find_element_by_id("id_file").send_keys('D:\\selenium_use_case\upload_file.txt')
          time.sleep(5)

          driver.quit()

          相關(guān)文章:

          輕松自動(dòng)化---selenium-webdriver(python) (八)



          posted on 2013-08-21 10:40 順其自然EVO 閱讀(320) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動(dòng)化測試學(xué)習(xí)

          <2013年8月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高唐县| 肇东市| 延安市| 新泰市| 建水县| 县级市| 濮阳县| 太谷县| 汕尾市| 华坪县| 科技| 普陀区| 博兴县| 临海市| 泸州市| 安岳县| 宁明县| 南昌市| 清流县| 河南省| 宁城县| 体育| 扶绥县| 池州市| 花莲县| 镇巴县| 泗洪县| 凤翔县| 福鼎市| 剑河县| 孟津县| 交口县| 修水县| 锦屏县| 河间市| 虹口区| 谷城县| 巴南区| 象州县| 蒙自县| 安多县|