Selenium上传文件

selenium自带了对应的API可以上传问题,如果这个上传文件的html code中显示的type是file那么你就可以使用下面的代码上传文件。

复制代码
/**
         * click the upload button to upload the file ,this is for hte webFile element ,the input type is file
         * @param driver
         * @param e
         * @param filepath
         * http://sauceio.com/index.php/2012/03/selenium-tips-uploading-files-in-remote-webdriver/
         * upload the local file from remote webdriver
         */
        public void uploadFile(WebElement e,String filepath){
            String uploadcode=getInnerHtmlCode(e);
            highLight(e);
            logger.info("the upload webelement html code we get is:"+uploadcode);
            e.sendKeys(filepath);
        }
复制代码

其中只需要填上对应的上传文件的路径即可,这个针对的是WebDriver实例的,如果是用的是RemoteWebDriver实例,那么你可能还需要设置一个属性如下(它的用途是可以识别运行的server端文件的文件为远程执行机器的路径,所以你可以直接上传文件):

driver.setFileDetector(new LocalFileDetector());
driver=new RemoteWebDriver("",capability);

 


本文转自hcy's workbench博客园博客,原文链接:http://www.cnblogs.com/alterhu/p/3357480.html ,如需转载请自行联系原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值