0

I would like to upload a file with selenium using JavaScript button

HTML code :

<div class="option optionFile">
<label class="editionFieldMandatory" for="file">File</label>
<input id="file_input" class="file" width="120" type="file" height="30" name="file_input" style="display: none;">
<object id="file_inputUploader" width="120" height="30" type="application/x-shockwave-flash" data="/scripts/external_api/uploadify.swf" style="visibility: visible;">
<param name="quality" value="high">
<param name="wmode" value="transparent">
<param name="allowScriptAccess" value="sameDomain">
<param name="flashvars" value="uploadifyID=file_input&pagepath=/content/&buttonText=Browse&script=/utils/upload_library.php?session_name=j2cnrk8ehssuhhi78gq2pdqoa1&folder=/var/www/BO/IHM/resources/documents/help_manuals&width=120&height=30&wmode=transparent&method=POST&queueSizeLimit=999&simUploadLimit=1&hideButton=true&fileDesc=Documents&fileExt=*.aac; *.aif; *.aiff; *.avi; *.bmp; *.conf; *.csv; *.doc; *.docx; *.dot; *.dotm; *.dotx; *.exe; *.flac; *.gif; *.htm; *.html; *.jpeg; *.jpg; *.log; *.lpcm; *.m2t; *.m2ts; *.mhtml; *.mid; *.mka; *.mkv; *.mov; *.mp3; *.mp4; *.mpeg; *.mpg; *.msi; *.ogg; *.pcm; *.pdf; *.png; *.pot; *.pps; *.ppt; *.pub; *.rar; *.tgz; *.tp; *.ts; *.txt; *.vdx; *.vob; *.vsd; *.vss; *.vst; *.vsx; *.vtx; *.wav; *.wma; *.wmv9; *.xls; *.xlsb; *.xlsm; *.xlsx; *.xml; *.zip&auto=true&sizeLimit=15728640&fileDataName=Filedata">
</object>
<div id="file_inputQueue" class="uploadifyQueue"></div>
</div>
</div>

I have a test but it fails:

WebElement elem = driver.findElement(By.xpath("//input[@id='file_input']"));
            elem.sendKeys("C:\test");

Tanks for your help.

PSS
  • 5,561
  • 5
  • 28
  • 30
Julien P.
  • 91
  • 1
  • 2
  • 8

2 Answers2

0

The problem is that your input for type="file" has a display:none style attached to it. If you remove it, selenium should be able to interact with it.

Farlan
  • 1,860
  • 2
  • 28
  • 37
  • Thanks.I can not change the software I use what I have marked – Julien P. Jun 03 '13 at 13:23
  • @JulienP. , if a user cannot interact with that specific element, neither can Selenium. – Arran Jun 03 '13 at 13:40
  • this is a hack, but you can use the following to set the element visible before trying to send keystrokes to it: `((JavascriptExecutor)driver).executeScript("document.getElementById('file_input').setAttribute('Style','display:block');","");` – Farlan Jun 03 '13 at 13:48
  • @Arran: It looks like the user doesn't interact directly with the input for `type="file"` - I guess the UI is in the flash object so that's what the script should strictly be trying to interact with. – Vince Bowdren Jun 03 '13 at 13:53
  • @vincebowdren your statement is incorrect. The test he is trying to run specifies the element as being identified by the xpath `//input[@id='file_input']` which is clearly in the html document, not flash. – Farlan Jun 03 '13 at 13:56
  • @Farlan I think that's why the test is wrong. If the OP wants the webdriver to use the web page in the same way that the user does, it will need to interact with the flash object instead of bypassing it and going direct to the `input` field. – Vince Bowdren Jun 03 '13 at 14:11
  • Thanks. @Farlan I have an error : JavascriptExecutor cannot be resolved to a type The problem is to validate because there is no submit button – Julien P. Jun 03 '13 at 15:06
  • you have to do `import org.openqa.selenium.JavascriptExecutor;` to be able to use `JavascriptExecutor` – Farlan Jun 04 '13 at 12:42
  • i had already but it's fail too – Julien P. Jun 04 '13 at 18:44
  • what version of FF and Selenium are you using? – Farlan Jun 04 '13 at 19:15
  • FF 13 and selenium 1.10 – Julien P. Jun 05 '13 at 08:07
0

It perfectly worked with JAVA. So you can try it for JS.

First make sure that the input element is visible

Don't click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.

Instead you can use:

driver.findElement(By.id("myUploadElement")).sendKeys("<absolutePathToMyFile>");

myUploadElement is the id of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you.

Keep in mind that the upload will work only If the element you send a file should be in the form <input type="file">

Kedar Tiwaskar
  • 1,292
  • 1
  • 10
  • 16
  • Thanks. I tried driver.findElement(By.id("file_input")).sendKeys("C:\test"); But its not working. – Julien P. Jun 04 '13 at 08:18
  • I wanted to test this script but its not working ((JavascriptExecutor)driver).executeScript("document.getElementById('file_input‌​').setAttribute('Style','display:block');",""); driver.findElement(By.id("file_input")).sendKeys("C:\test"); – Julien P. Jun 04 '13 at 08:27
  • FF 13 and i have selenium 2.0 – Julien P. Jun 05 '13 at 08:08
  • Just use the path as **C:\\test\\.extension** instead of using **"C:\test"** – Kedar Tiwaskar Jun 06 '13 at 12:48