1. 程式人生 > >robotframework+selenium2library之上傳本地檔案

robotframework+selenium2library之上傳本地檔案

針對將本地的檔案上傳到測試系統,selenium2library提供了一個關鍵詞

choose file

官方給定的解釋如下:

Source:
Selenium2Library <test library>
Arguments:
[ locator | file_path ]
Inputs the `file_path` into file input field found by `identifier`.
This keyword is most often used to input files into upload forms. The file specified with `file_path` must be available on the same host where the Selenium Server is running.
Example:
Choose File my_upload_field  /home/user/files/trades.csv

一開始以為進入到上傳頁面,點選瀏覽,再使用choose file,結果發現,這個函式包含了 點選瀏覽--選中檔案  那麼點選瀏覽就是多餘的操作了 


在該頁面,就只需要執行choose  就可以將圖片選中,執行的具體命令為:

choose file    xpath=//div[@class='pd6 dot fcb']/span/input    C:\\Documents and Settings\\zhouxuan\\My Documents\\My Pictures\\bug5.png

其中xpath為選中瀏覽圖示的路徑,而 C:\\Documents and Settings\\zhouxuan\\My Documents\\My Pictures\\bug5.png(此處的\\為轉義用,當只用\時會出現錯誤)則為需要上傳的圖片的路徑。

該操作執行完畢後,則會顯示為:


圖片已經順利載入進來了,後續的操作就很簡單了。

ps:

一開始點選上傳圖片的按鍵的時候,一直失敗,各種除錯和轉換路徑,但是都失敗,後來換到同事的環境下執行,一切OK,檢視火狐的版本,我的是35.0版本,他的是22.0.後來我也火狐瀏覽器的版本降低到22.0.指令碼執行正常了。想想之前有時候在火狐下執行會導致瀏覽器不響應,也可能和瀏覽器相關。今天應該解決了。