简体   繁体   中英

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. 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.
With Selenium you should bot click on dialog, open OS browsing dialog, select file and click "upload" button.
With Selenium uploaded file should be sent to a special element presented on the page.
This is not a visible element.
It can be located by following XPath: "//input[@type='file']" . The same with 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM