8

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.

drewboy
  • 81
  • 1
  • 2

3 Answers3

5

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");
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
nilesh
  • 14,131
  • 7
  • 65
  • 79
3

Uploading file is usually a POST request, so you actually can upload a file without using Selenium; Unless your site requires cookies, then you need to reconstruct the cookies with webdriver.get_cookies() first

A simple example:

# required package:
#   http://pypi.python.org/pypi/MultipartPostHandler/0.1.0

import MultipartPostHandler, urllib2, cookielib

cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
                              MultipartPostHandler.MultipartPostHandler)

path_to_file = r"abc.zip"

open_file = open(path_to_file,"rb")
param = { "file": open_file }
req = opener.open("http://www.yoursite.com/uploadfile", param)
open_file.close()
EwyynTomato
  • 4,009
  • 1
  • 31
  • 39
-1

Using IJavaScriptExecutor is to change the upload input field to click able so chrome driver won't pop-up error saying this element is non clickable.

        [SetUp]
        public void SetupTest()
        {
            driver = new ChromeDriver();
            baseURL = "";
            verificationErrors = new StringBuilder();
        }

        [Test]
        public void Test()
        {
            IJavaScriptExecutor js = driver as IJavaScriptExecutor;
            IWebElement element = driver.FindElement(By.Id("UploadFile_ButtonID"));
            js.ExecuteScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", element);
            Thread.Sleep(1000);
            element.SendKeys("D:\\path\\test\\image.jpg");
}
Vitus
  • 1
  • 1
  • 1
    Code without context is pointless. You haven't explained why you are using a JavaScript Executor to set the element visible and IMHO it's probably bad practise. Selenium prevents you from interacting with hidden elements because that is something an end user could not do. Rather than blindly using JavaScript to force the element visible you should in reality do what an end user would do to make the element visible. – Ardesco Mar 18 '13 at 09:00
  • 1
    You also use a Thread.sleep which is again bad practise, the correct thing to do would be to use a WebDriverWait object and the ExpectedConditions class to wait for the element to become visible. This is a shoddy example with no explanation and bad code practise -1 – Ardesco Mar 18 '13 at 09:00