qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Selenium文件上傳的實現

           一、對于上傳文件, 從手動操作我們可以看出, 需要對window 窗體進行操作, 而對于selenium webdriver 在這方面應用就受到了限制。 但是, 慶幸的是, 對于含有input element的上傳, 我們可以直接通過sendkeys來傳入文件路徑,省略了對window 窗體的操作來實現文件上傳, 具體實現過程如下:
            1)找到上傳控件element,并輸入路徑:
            WebElement element = driver.findElement(By.id("cloudFax-attachment-form-upload-input"));
            element.sendKeys(getFilePath(text.txt));
            2)路徑的處理:
          private String getFilePath(String resource) {
          URL path = this.getClass().getResource(resource);
          return path.toString().replaceAll("file:/","");
          }
            這樣把代碼和文件上傳到服務器, 就可以找到該文件進行上傳。
            這里需要注意的幾點:
            The element you want to put filepath is the "whole" input element rather than the "readonly" input element,as below, the first locator is right but the second locator will throw an exception.
            直接調用element.sendkeys , 不需要再做一次element.clear(),否則會出現該異常:Element must be user-editable in order to clear it.
            二、對于如下控件的上傳方式, 暫時無法實現:



          posted on 2014-04-18 13:50 順其自然EVO 閱讀(2811) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

          <2014年4月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高陵县| 金坛市| 修文县| 宕昌县| 上虞市| 新乡县| 司法| 新泰市| 于田县| 分宜县| 电白县| 双桥区| 昌都县| 玉林市| 布拖县| 清水县| 城固县| 大关县| 噶尔县| 开封市| 永安市| 安国市| 德钦县| 隆安县| 东港市| 紫阳县| 漳浦县| 德格县| 佛冈县| 利辛县| 平乐县| 萨迦县| 抚顺县| 慈利县| 云林县| 延吉市| 武义县| 泗阳县| 湖州市| 淮北市| 桐乡市|