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 閱讀(2812) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 辽宁省| 陇西县| 车险| 福清市| 巴青县| 泌阳县| 迁安市| 丹棱县| 华池县| 任丘市| 唐海县| 澳门| 长海县| 得荣县| 澄江县| 米林县| 师宗县| 桑日县| 石阡县| 台江县| 景泰县| 仁布县| 福清市| 泊头市| 册亨县| 金山区| 梁河县| 祁门县| 金沙县| 五常市| 湛江市| 棋牌| 胶州市| 丹寨县| 丰台区| 克山县| 凤台县| 灵璧县| 德兴市| 繁昌县| 宣城市|