qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          Selenium文件上傳的實現(xiàn)

           一、對于上傳文件, 從手動操作我們可以看出, 需要對window 窗體進行操作, 而對于selenium webdriver 在這方面應用就受到了限制。 但是, 慶幸的是, 對于含有input element的上傳, 我們可以直接通過sendkeys來傳入文件路徑,省略了對window 窗體的操作來實現(xiàn)文件上傳, 具體實現(xiàn)過程如下:
            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:/","");
          }
            這樣把代碼和文件上傳到服務(wù)器, 就可以找到該文件進行上傳。
            這里需要注意的幾點:
            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.
            直接調(diào)用element.sendkeys , 不需要再做一次element.clear(),否則會出現(xiàn)該異常:Element must be user-editable in order to clear it.
            二、對于如下控件的上傳方式, 暫時無法實現(xiàn):



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

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

          導航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 余干县| 广丰县| 化州市| 巴彦淖尔市| 关岭| 县级市| 云梦县| 河曲县| 兴山县| 达孜县| 阜新市| 三门县| 静安区| 平邑县| 汶川县| 东光县| 手游| 斗六市| 库伦旗| 九台市| 康平县| 沭阳县| 晋城| 通江县| 蒙城县| 翼城县| 永川市| 东宁县| 泾川县| 柳江县| 霸州市| 陇南市| 新乡县| 贡山| 安义县| 临夏市| 收藏| 莱阳市| 商水县| 攀枝花市| 麻阳|