简体   繁体   English

在 Java selenium 测试用例中使用打开文件对话框

[英]Open file dialog using in Java selenium test case

In my angular application, I am writing test cases using selenium. In one of the test suite, I have a scenario where on click of a button, I need to open a Windows FileInput dialog and select a file that is passed and read the data in the file.在我的 angular 应用程序中,我正在使用 selenium 编写测试用例。在其中一个测试套件中,我有一个场景,点击一个按钮,我需要打开一个 Windows FileInput 对话框和 select 一个传递的文件并读取数据在文件中。 How can I achieve this.我怎样才能做到这一点。 I am using like this but nothing happens.我正在这样使用,但没有任何反应。 How can I achieve this?我怎样才能做到这一点?

driver = Chrome webdriver;
element = driver.find_element_by_id("fileUpload")
element.send_keys("myfile.txt")

Uploading file with Selenium is done not the same way human user does.使用Selenium上传文件的方式与人类用户不同。
With Selenium you should bot click on dialog, open OS browsing dialog, select file and click "upload" button.对于 Selenium,您应该点击对话框,打开操作系统浏览对话框,select 文件,然后点击“上传”按钮。
With Selenium uploaded file should be sent to a special element presented on the page.使用 Selenium 上传的文件应发送到页面上显示的特殊元素。
This is not a visible element.这不是可见元素。
It can be located by following XPath: "//input[@type='file']" .可以通过以下 XPath: "//input[@type='file']"找到。 The same with CSS Selector "input[type='file']" .与 CSS Selector "input[type='file']"相同。
Full absolute path to uploaded file should be sent to that element.上传文件的完整绝对路径应发送到该元素。
Like the following:像下面这样:

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:/path/to/file.extension");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java Selenium中执行测试案例后打开/关闭浏览器 - Open/Close browser after test case execution in Java Selenium 如何通过命令行使用Selenium RC运行Selenium Java测试用例和套件 - How to run selenium java test case and suite using selenium rc through command line 使用 Selenium Java 测试用例调用 CLICK 时获取“org.openqa.selenium.ElementClickInterceptedException” - Getting "org.openqa.selenium.ElementClickInterceptedException" when invoking a CLICK using Selenium Java Test case 如何使用 selenium 在 IE 中记录测试用例? - How to record test case in IE using selenium? 为每个测试用例打开一个新的浏览器是一种好习惯吗? (硒,JUnit) - Is it good practice to open a new browser for every test case? (Selenium, JUnit) Selenium Webdriver(Java)-文件下载对话框 - Selenium webdriver (java) - file download dialog 如何使用 spring/java 在独立服务器中运行记录的 selenium 测试用例 - How to run recorded selenium test case in standalone servcer using spring/java 无法使用 selenium Java 调用对 TestNG 测试用例执行鼠标操作的方法 - Unable to call method which performing mouse actions to TestNG test case using selenium Java 使用 Java 在 safari 浏览器中运行 Selenium Web 驱动程序测试用例时出错? - Getting error while running Selenium web-driver test case in safari browser using Java? 如何使用Java Selenium处理确认对话框 - How handle confirmation dialog using java selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM