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);

 

posted @   高级测试开发网  阅读(1322)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
了解开发资源最新动态:https://seniortesting.club
点击右上角即可分享
微信分享提示