上傳文件:
第一種方式,sendkeys(),最簡單的
#encoding=utf-8
from selenium import webdriver
import unittest
import time
import traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
class TestDemo(unittest.TestCase):
def setUp(self):
# 啟動Chrome瀏覽器
#self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")
self.driver = webdriver.Firefox(executable_path = "D:\\geckodriver")
def test_uploadFileBySendKeys(self):
url = "http://127.0.0.1/test_upload_file.html"
# 訪問自定義網頁
self.driver.get(url)
try:
# 創建一個顯示等待對象
wait = WebDriverWait(self.driver, 10, 0.2)
# 顯示等待判斷被測試頁面上的上傳文件按鈕是否處於可被點擊狀態
wait.until(EC.element_to_be_clickable((By.ID, 'file')))
except TimeoutException, e:
# 捕獲TimeoutException異常
print traceback.print_exc()
except NoSuchElementException, e:
# 捕獲NoSuchElementException異常
print traceback.print_exc()
except Exception, e:
# 捕獲其他異常
print traceback.print_exc()
else:
# 查找頁面上ID屬性值為“file”的文件上傳框
fileBox = self.driver.find_element_by_id("file")
# 在文件上傳框的路徑框里輸入要上傳的文件路徑“d:\\test.txt”
fileBox.send_keys("d:\\test.txt")
# 暫停查看上傳的文件
time.sleep(4)
# 找到頁面上ID屬性值為“filesubmit”的文件提交按鈕對象
fileSubmitButton = self.driver.find_element_by_id("filesubmit")
# 單擊提交按鈕,完成文件上傳操作
fileSubmitButton.click()
# 因為文件上傳需要時間,所以這里可以添加顯示等待場景,
# 判斷文件上傳成功后,頁面是否跳轉到文件上傳成功的頁面。
# 通過EC.title_is()方法判斷跳轉后的頁面的Title
# 值是否符合期望,如果匹配將繼續執行后續代碼
#wait.until(EC.title_is(u"文件上傳成功"))
def tearDown(self):
# 退出IE瀏覽器
self.driver.quit()
if __name__ == '__main__':
unittest.main()
D:\test>python test.py
.
----------------------------------------------------------------------
Ran 1 test in 37.512s
OK
第二種方式,操作鍵盤
點擊后彈出一個文件上傳框,在文件名的輸入框里,直接輸入文件名,然后enter,
#encoding=utf-8
from selenium import webdriver
import unittest
import time
import traceback
import win32clipboard as w
import win32api
import win32con
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
# 用於設置剪切板內容
def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
w.CloseClipboard()
# 鍵盤按鍵映射字典
VK_CODE = {
'enter':0x0D,
'ctrl':0x11,
'v':0x56}
# 鍵盤鍵按下
def keyDown(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)
# 鍵盤鍵抬起
def keyUp(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0)
class TestDemo(unittest.TestCase):
def setUp(self):
# 啟動Chrome瀏覽器
#self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
self.driver = webdriver.Firefox(executable_path = "d:\\geckodriver")
def test_uploadFileByKeyboard(self):
url = "http://127.0.0.1/test_upload_file.html"
# 訪問自定義網頁
self.driver.get(url)
try:
# 創建一個顯示等待對象
wait = WebDriverWait(self.driver, 10, 0.2)
# 顯示等待判斷被測試頁面上的上傳文件按鈕是否處於可被點擊狀態
wait.until(EC.element_to_be_clickable((By.ID, 'file')))
except TimeoutException, e:
# 捕獲TimeoutException異常
print traceback.print_exc()
except NoSuchElementException, e:
# 捕獲NoSuchElementException異常
print traceback.print_exc()
except Exception, e:
# 捕獲其他異常
print traceback.print_exc()
else:
# 將即將要上傳的文件名及路徑設置到剪切板中
setText(u"c:\\test.txt")
# 查找頁面上ID屬性值為“file”的文件上傳框,
# 並點擊調出選擇文件上傳框
self.driver.find_element_by_id("file").click()
time.sleep(2)
# 模擬鍵盤按下ctrl + v組合鍵
keyDown("ctrl")
keyDown("v")
# 模擬鍵盤釋放Ctrl + v組合鍵
keyUp("v")
keyUp("ctrl")
time.sleep(1)
# 模擬鍵盤按下回車鍵
keyDown("enter")
# 模擬鍵盤釋放回車鍵
keyUp("enter")
# 暫停查看上傳的文件
time.sleep(2)
# 找到頁面上ID屬性值為“filesubmit”的文件提交按鈕對象
fileSubmitButton = self.driver.find_element_by_id("filesubmit")
# 單擊提交按鈕,完成文件上傳操作
fileSubmitButton.click()
# 因為文件上傳需要時間,所以這里可以添加顯示等待場景,
# 判斷文件上傳成功后,頁面是否跳轉到文件上傳成功的頁面。
# 通過EC.title_is()方法判斷跳轉后的頁面的Title
# 值是否符合期望,如果匹配將繼續執行后續代碼
#wait.until(EC.title_is(u"文件上傳成功"))
#def tearDown(self):
# 退出IE瀏覽器
#self.driver.quit()
if __name__ == '__main__':
unittest.main()
D:\test>python test.py
.
----------------------------------------------------------------------
Ran 1 test in 29.442s
OK
第三種用Autoit使用
盡量用前兩種,autoit是用來解決疑難雜症的問題,爛七八糟的windows彈窗,都可以用autoit來進行操作,比如輸入、選擇、按鍵。
測試簡單的windows小程序的,
兩個都要裝,
如果windows彈出很多窗口,用autoit來識別,進行處理
安裝文件autoit-v3-setup.exe、SciTE4AutoIt3.exe
用腳本標記器SciTe把test.au3的腳本用compile script to exe工具轉成exe文件

test.au3腳本內容是用來在window彈出選擇框的時候,進行自動選擇文件的

用下邊的工具把test.au3腳本convert成exe文件,以便python調用

然后把腳本文件里的路徑改一下
#encoding=utf-8
from selenium import webdriver
import unittest
import time, os
import traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
class TestDemo(unittest.TestCase):
def setUp(self):
# 啟動Chrome瀏覽器
self.driver = webdriver.Firefox(executable_path = "d:\\geckodriver")
def test_uploadFileByAutoIt(self):
url = "http://127.0.0.1/test_upload_file.html"
# 訪問自定義網頁
self.driver.get(url)
try:
# 創建一個顯示等待對象
wait = WebDriverWait(self.driver, 10, 0.2)
# 顯示等待判斷被測試頁面上的上傳文件按鈕是否處於可被點擊狀態
wait.until(EC.element_to_be_clickable((By.ID, 'file')))
except TimeoutException, e:
# 捕獲TimeoutException異常
print traceback.print_exc()
except NoSuchElementException, e:
# 捕獲NoSuchElementException異常
print traceback.print_exc()
except Exception, e:
# 捕獲其他異常
print traceback.print_exc()
else:
# 查找頁面上ID屬性值為“file”的文件上傳框,
# 並點擊調出選擇文件上傳框
self.driver.find_element_by_id("file").click()
# 通過Python提供的os模塊的system方法執行生成的test.exe文件
os.system("d:\\test\\test.exe")
# 由於AutoIt腳本轉換后的可執行文件test.exe可能執行速度比較慢,
# 這里等待5秒,以確保test.exe腳本執行成功
time.sleep(5)
# 找到頁面上ID屬性值為“filesubmit”的文件提交按鈕對象
fileSubmitButton = self.driver.find_element_by_id("filesubmit")
# 單擊提交按鈕,完成文件上傳操作
fileSubmitButton.click()
# 因為文件上傳需要時間,所以這里可以添加顯示等待場景,
# 判斷文件上傳成功后,頁面是否跳轉到文件上傳成功的頁面。
# 通過EC.title_is()方法判斷跳轉后的頁面的Title
# 值是否符合期望,如果匹配將繼續執行后續代碼
#wait.until(EC.title_is(u"文件上傳成功"))
time.sleep(2)
def tearDown(self):
# 退出IE瀏覽器
self.driver.quit()
if __name__ == '__main__':
unittest.main()
D:\test>python test.py
.
----------------------------------------------------------------------
Ran 1 test in 43.298s
OK
