7

In my selenium c# windows forms application, I have a scenario like uploading a doc/docx file. I have already done uploading in some another case.since it has some text box infront of browse button I uploaded using

IWebElement element = driver.FindElement(By.Id("uploadhere"));
element.SendKeys("C:\\Some_Folder\\MyFile.txt");

But here now in my present scenario, I cant use this code. My picture below shows the upload file dialog box. I googled many and couldnt find one that suits my need...I can click on browse button but cant select a file. Firebug and selenium IDE seemed not to be useful..

Can anyone help me on this? Any comments would be really appreciated..enter image description here

vysakh
  • 266
  • 2
  • 4
  • 19
  • I assume this is in Firefox? What errors are you getting? What is the HTML surrounding that upload control? – Arran Sep 18 '13 at 08:21
  • @Arran:You are right..No errors...even i am not able to use the firebug or selenium RC for getting the attributes... – vysakh Sep 18 '13 at 08:38
  • Possible duplicate of [Webdriver: File Upload](https://stackoverflow.com/questions/3300580/webdriver-file-upload) – Ashish Kamble Oct 08 '18 at 12:07

5 Answers5

1

You can use windows forms commands to navigate the upload box that appears. I handled it by using keys to change the focus of the window to where I need it and send a string with the path to the file I wanted to upload. See code below:

//Click browse btn to open upload window
     driver.FindElement(By.ClassName("uploadbtn")).Click();

//Wait for Upload Window to appear(may not be necessary in some cases)
     Thread.Sleep(2000);

//Send the path and then click the Right arrow key
     SendKeys.SendWait(GetProjectRoot() + @"\DataFiles\red.png" + @"{RIGHT}");

//Click Enter Key to submit
     SendKeys.SendWait(@"{Enter}");

Be mindful if you are using a console application, there are some steps you would need to do to get "using System.Windows.Forms;" to work. The idea is to use the keys to navigate the upload window. This method also only works if you have the path to the file you need to upload.

Isaac M
  • 56
  • 4
0

Since its a OS dialog box you cannot handle it with selenium , you can use java script executor check this link Webdriver: File Upload

Community
  • 1
  • 1
  • But in this line ((IJavascriptExecutor)driver).executeScript(script); I am getting an error like The type or namespace name 'IjavasscriptExecutor' could not be found..why it is so? I have already declared IjavascriptExecutor as IJavaScriptExecutor js = driver as IJavaScriptExecutor; and tried with js.executescript(script);but that also doesnt help me.. – vysakh Sep 18 '13 at 11:13
  • Your javascriptExecutor is declared js you are using differnt name i think ... try js.ExecuteScript(script) – Praveen Kumar Sep 18 '13 at 11:29
  • already told u its also not working..when it is tried, error is the name js does not exist in the current context..whereas i have declared the same when the program gets initialised.. – vysakh Sep 18 '13 at 11:31
  • now the error is gone...but can u please make me clear,what is this document in the link you gave me? – vysakh Sep 18 '13 at 11:38
  • check this link it explains basic thing about js http://www.w3schools.com/jsref/dom_obj_document.asp as I am not well versed in Javascript – Praveen Kumar Sep 18 '13 at 11:45
  • but its not working..@ runtime it shows an error like waiting for evaluate.js load failed (UnexpectedJavaScriptError) – vysakh Sep 18 '13 at 11:59
  • what is the script you are executing...? – Praveen Kumar Sep 18 '13 at 12:36
0

If the Webpage have in HTML something like this:

<input type="file" id=.....

than you can use the following construct in C#:

string File_with_FullPath="C:\Some_Folder\MyFile.txt";
driver.FindElement(By.XPath("//input[@type='file']")).SendKeys(File_with_FullPath);

This prevent you from opening a not needed File-Explorer and all of the not needed problems with this.

Hope it helps.

Dragon
  • 49
  • 7
0

Not sure how obsolete this issue is, but just in case if normal SendKeys approach don't work, use OpenDialogWindowHandler package which is available in NuGet Package Manager

using OpenDialogWindowHandler;

// Find "upload" or "browse files" button. Click on it.
 var upload = _driver.FindElement(By.XPath("//*[text()='Browse files']"));
 upload.Click();

// using "HandleOpenDialog" to locate and upload file 
 HandleOpenDialog hndOpen = new HandleOpenDialog();  
 hndOpen.fileOpenDialog(photoLocation, photoName); 
 
-2

We can Not handle windows fileOpenDialog through Selenium. we have to use either Javascript executor but some time still it is not possible to use Javascriptexecutor. it won't execute. It gives null value error. Example :

If you go to naukri.com on below link http://my.naukri.com/manager/createacc2.php?othersrcp=11499&wExp=N or

<input type="file" id="browsecv" name="browsecv"></input>

to upload the file.

your javascript and selenium wont identify the browse element.

Then we have to use third party tool such as Point Position to compute x & Y coordinate of "browsefile" Button then, we can use C#.Net low level MouseClick handle to click on it or we can use AutoIt tool to handle wondows pop. To know more you can visit on

http://avinashpandeblogsonseleniumautomation.blogspot.in/2015/06/upload-file-using-selenium-web-driver.html you will get solution with an example.

Avinash Pande
  • 1,510
  • 19
  • 17