Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: Upload file in Google Chrome

Is there any way to upload file in Google Chrome since Selenium RC "attach_file" only supports *Firefox? Any suggestion or workarounds are much appreciated.

like image 288
drewboy Avatar asked Mar 12 '12 10:03

drewboy


People also ask

Can Selenium upload file?

The most basic way of uploading files in Selenium is using the sendKeys method. It is an inbuilt feature for file upload in Selenium.

How do I automate a file to upload?

You can call sendKeys() on the file control to fill the file control with file path and then submit form. With this approach, no file dialog will be shown, you just bypass the file dialog and set the file path directly in the file upload control.


1 Answers

If you are using Webdriver then to upload file all you need is use "sendKeys" to type the file path. You need to 'skip' the part of clicking on the browse button that opens a dialog box to select the file. A Java version that works for me looks something like below,

WebElement inputFilePath = driver.findElement(By.id("filepath"));
inputFilePath.sendKeys("/absolute/path/to/my/local/file");
like image 75
nilesh Avatar answered Oct 15 '22 07:10

nilesh