如何使用Java中的Selenium WebDriver上传文件 [英] How to upload file using Selenium WebDriver in Java

查看:923
本文介绍了如何使用Java中的Selenium WebDriver上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能让我知道如何使用Selenium通过Java代码上传文件吗?

Can anyone let me know how to upload a file using Selenium by Java code?

当我点击应用程序中的按钮时,它会在新窗口中打开我可以用来选择上传文件。由Silverlight(C#)开发的浏览按钮。

When I click on button in the application it gets open in the new window what I can use to select upload file. The browse button developed by Silverlight (C#).

推荐答案

首先确保输入元素可见

正如Mark Collin在讨论中所述这里

As stated by Mark Collin in the discussion here:


不要点击浏览按钮,它会触发操作系统级别对话
框并有效地阻止你的测试死亡。

Don't click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.

相反,你可以使用:

driver.findElement(By.id(myUploadElement))。sendKeys(< absolutePathToMyFile>);

myUploadElement 是该元素的ID(本例中为按钮),在sendKeys中,您必须指定所需内容的绝对路径上传(图片,视频等)。 Selenium将为您完成剩余工作。

myUploadElement is the id of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you.

请记住,上传仅适用于如果您发送文件的元素应采用<$ c格式$ c>< input type =file>

Keep in mind that the upload will work only If the element you send a file should be in the form <input type="file">

这篇关于如何使用Java中的Selenium WebDriver上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆