I have a HTML page with button named "Upload" and id: btn-import-questions
. The element:
<button class="btn btn-success btn-sm col-lg-11" id="btn-import-questions" data-ts-file-selector="questions-import-init"> Upload <i class="fa fa-upload"></i></button>
I tried a Selenium Java code like this:
driver.findElement(By.id("btn-import-questions")).sendkeys("C:/path/to/file.xlsx");
But since this is an upload button and not an input-type element, the above code is not working.
We can upload files using Selenium Webdriver. This is achieved by the sendKeys method. We have to first identify the element which performs the file selection by mentioning the file path [to be uploaded].
Check the DOM because somewhere there must be an <input type="file">
. The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path. With Selenium the same can be achieved with the .sendkeys():
driver.findElement(By.xpath("//input[@type=\"file\"]")).sendkeys(localFilePath);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With