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

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

问题描述

谁能告诉我如何通过 Java 代码使用 Selenium 上传文件?

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

解决方案

首先确保输入元素可见

正如 Mark Collin 在此处的讨论中所述:

<块引用>

不要点击浏览按钮,它会触发一个操作系统级别的对话框并有效地阻止您的测试.

相反,您可以使用:

driver.findElement(By.id("myUploadElement")).sendKeys("");

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

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

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

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#).

解决方案

First make sure that the input element is visible

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.

Instead you can use:

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

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.

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天全站免登陆