Using Webdriver and Javascript for a hidden file upload element

2,715 views
Skip to first unread message

Avishek Datta

unread,
Jul 19, 2013, 3:09:25 AM7/19/13
to webd...@googlegroups.com

I am trying to automate a file upload scenario in a client web application. The code of the entire file upload form looks like this

<td valign="top"> <iframe id="batchLoad:inputFile:uploadFrame" class="iceInpFile" width="600px" scrolling="no" height="30px" frameborder="0" title="Input File Frame" style="border-collapse:collapse; border-spacing:0px; padding:0px;" src="/hip-webapp/block/resource/LTExMzg4MjQzMTY=/" name="batchLoad:inputFile:uploadFrame" marginwidth="0" marginheight="0" allowtransparency="true"> <html style="overflow:hidden;"> <head> <body style="background-color:transparent; overflow:hidden"> <form id="fileUploadForm" enctype="multipart/form-data" action="/hip-webap/uploadHtml" method="post"> <input type="hidden" value="batchLoad:inputFile" name="ice.component"> <input type="hidden" value="3" name="ice.view"> <input class="iceInpFileTxt" type="file" size="35" name="upload"> <input class="iceInpFileBtn" type="submit" value="Upload"> </form> </body> </html> </iframe> <br> <span id="batchLoad:j_id537"></span> </td>

I tried with the conventional File upload method but that did not work. I am not much familiar with Javascript hence I think I am doing some syntax error. What I tried is:

String ew = (String)js.executeScript("document.getElementByXPath('//form[@id='fileUploadForm']/input[3]')"); String j = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';"; js.executeScript(j, ew);
I got the Xpath using Selenium IDE. I have also tried this but that did not work either.

((JavascriptExecutor)driver).executeScript("document.getElementByClassName(iceInpFileTxt).style.visibility = 'visible';"); ((JavascriptExecutor)driver).executeScript("document.getElementByClassName('iceInpFileTxt').value = 'D:\\AD\\Prac\\Prac\\002 EditPrac Add Person Error.xml-revHEAD.svn000.tmp.xml'");

Please advice. What I intend to do is unhide the file upload element and sendkeys the absoulute path of the xml file.

darrell

unread,
Jul 19, 2013, 2:35:34 PM7/19/13
to webd...@googlegroups.com
WebDriver only interacts with visible elements. It also does NOT work with file picker dialogs. If you manually test the application, you open the file picker dialog, find the path to the XML file and click the Open button. The file picker will then populate the hidden input. Since WebDriver cannot automate the file picker, I just inject the path to the XML file into the hidden input. You can either use a JavascriptExecutor to inject the filename into the hidden input or you can use a JavascriptExecutor to make the hidden input visible then use WebDriver to sendKeys() the filename to the, not visible, input.

Either way will work. Since the file picker isn't part of your application (it is just a library you call), you don't need to use it. Just go around it.

Anand

unread,
Jul 19, 2013, 7:09:04 PM7/19/13
to webd...@googlegroups.com
darrell,

Just curious to know, why would we need to make it visible first? I am bit confused. Don't we sendKeys the entire file path of the file to be uploaded to the input element with type="file" ? That input is not hidden. Am I missing something? 
I thought, in the above example, we would sendKeys to $$("input.iceInpFileTxt[name='upload']"). 

susanth.nair

unread,
Jul 20, 2013, 11:19:03 AM7/20/13
to webd...@googlegroups.com
sendkey is the Replica of user action were setting value is programatic way. some time based on keyboard events some javascript may trigger based on client side dev logic so sendKeys is the correct way


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

darrell

unread,
Jul 21, 2013, 12:29:44 PM7/21/13
to webd...@googlegroups.com
My mistake. I incorrectly thought the INPUT with type='file' was hidden. Since it is not hidden there is no need to make it visible.
Reply all
Reply to author
Forward
0 new messages