Quality is delighting customers
Tags:
I wouldn't recommend going for AutoIT as it will require the browser constantly being in focus therefore you will not be able to do anything else while your test is running. Moreover when you will be running your tests using Selenium Grid in parallel you will face issues.
Therefore you can just locate the relevant <input type="file"> element and invoke sendKeys( ) function providing the full path to the file you're trying to upload.
Relevant code:
driver.findElement(By.xpath("//input[@type='file'")).sendKeys("C:\\Users\\Desktop\\AutoIT\\upload.png");
More information: How can I work with file uploads during a Webdriver test?
Hi Dmitri,
Thanks for your quick reply.
I have tried with sendkeys, it is giving me below error.
Error:
org.openqa.selenium.WebDriverException: The element is not focusable (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
Driver info: org.openqa.selenium.edge.EdgeDriver
Capabilities {InPrivate: false, acceptSslCerts: true, applicationCacheEnabled: true, browserName: MicrosoftEdge, browserVersion: 42.17134.1.0, javascriptEnabled: true, locationContextEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10, takesElementScreenshot: true, takesScreenshot: true, webStorageEnabled: true}
My Code:
WebElement upload = driver.findElement(By.xpath("//input[@id="upload"]");
upload.sendKeys("C:\\images\\im.png");
Hi Madhavi,
As per most of the professionals working with organizations providing software testing solutions, this should always work by using sendkeys(), but unfortunately that's not working in your case.
I would recommend you to go for the following alternative.
This can be done using Robot class and keypress events.
import java.awt.Robot;
driver.findElement(By.xpath("//input[@type='file'")).click();
setClipboardData("C:\\images\\im.png");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Hope it helps.
Thanks,
Anand
© 2021 Created by Quality Testing.
Powered by