Selenium: upload file to Google Chrome

Is there a way to upload a file to Google Chrome since Selenium RC "attach_file" only supports Firefox? Any suggestion or workarounds are very welcome.

+7
source share
3 answers

If you use Webdriver, then to download the file you need to use "sendKeys" to enter the path to the file. You need to โ€œskipโ€ the part of pressing the browse button, which opens a dialog box for selecting a file. The Java version that works for me looks something like this:

WebElement inputFilePath = driver.findElement(By.id("filepath")); inputFilePath.sendKeys("/absolute/path/to/my/local/file"); 
+4
source

Uploading a file is usually a POST request, so you can upload a file without using Selenium; If your site does not need cookies, you need to restore cookies using 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() 
+3
source

Using IJavaScriptExecutor is to change the input field for loading to click so that the chrome driver does not throw an error saying that this element is not 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"); } 
-one
source

All Articles