[英]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.