輕松自動化---selenium-webdriver(python) (九)
本節重點:
上傳文件
文件上傳操作也比較常見功能之一,上傳功能沒有用到新有方法或函數,關鍵是思路。
上傳過程一般要打開一個本地窗口,從窗口選擇本地文件添加。所以,一般會卡在如何操作本地窗口添加上傳文件。
其實,在selenium webdriver 沒我們想的那么復雜;只要定位上傳按鈕,通send_keys添加本地文件路徑就可以了。絕對路徑和相對路徑都可以,關鍵是上傳的文件存在。下面通地例子演示。
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 driver = webdriver.Firefox() #腳本要與upload_file.html同一目錄 #定位上傳按鈕,添加本地文件 driver.quit() |
其它有些應用不好找,所以就自己創建頁面,這樣雖然麻煩,但腳本代碼突出重點。
這里找一139郵箱的實例,有帳號的同學可以測試一下~!
(登陸基礎版的139郵箱,網盤模塊上傳文件。)
139upload.py
#coding=utf-8 driver = webdriver.Firefox() driver.get(http://m.mail.10086.cn) #登陸 #進入139網盤模塊 #上傳文件 driver.quit() |
相關文章:
輕松自動化---selenium-webdriver(python) (八)
posted on 2013-08-21 10:40 順其自然EVO 閱讀(320) 評論(0) 編輯 收藏 所屬分類: selenium and watir webdrivers 自動化測試學習