Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a path for a file to upload if an <input> element is not visible and only reference by an anchor tag <a>?

With WebDriver if the element is of type="file" I can usually just to a .sendKeys() directly to the element and it works beautifully.

Unfortunately the situation I have now there is a custom button used that has an anchor tag referring to the input element:

//anchor tag which is a custom button referring to non visible file input element
    <a id="image-upload-button" class="control_button button" href="#/create_channel/addImage" data-ember-action="151">

//refers to this file input element which is not visible
    <input id="image-selector-button" name="image-selector" type="file" class="selectImageBtn" {{action "selectImage" on="change" target="view"}} {{action "onBlur" on="blur" target="App.router.imageSelectorView"}} {{action "onFocus" on="focus" target="view"}}/>

In this case sendKeys() does not work on the anchor tag:

driver.findElement(By.id("image-upload-button ")).sendKeys(“c:\\myFile.bmp”);

Error: org.openqa.selenium.WebDriverException: focusElement execution failed; Failed to send keys because cannot focus element

Nor does sendKeys() work the non-visible file input element:

driver.findElement(By.id("image-selector-button ")).sendKeys(“c:\\myFile.bmp”);

Error: org.openqa.selenium.ElementNotVisibleException: Element must be displayed to click

I've tried injecting javaScript to set the path value on the non-visible input element a few different ways (see below), but nothing seems to work. Any ideas on how I can set this path?

//nothing happens when I try this

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.document.getElementById('image-selector-button').setAttribute('value','C:\\MyFile.jpg');");  



//I tried firing a change even first as well and that didn't appear to do anything either

js.executeScript(
"var el=document.getElementById('image-selector-button'); " +
" function fire(evttype) { " +
" if (document.createEvent) { " +
" var evt = document.createEvent('HTMLEvents'); " +
" evt.initEvent( evttype, false, false); "+
" el.dispatchEvent(evt); " +
" } else if (document.createEventObject) { "+
"  el.fireEvent('on' + evttype); " +
" }    }; " +
" fire(el,'change'); " );

Any help or suggestions would be greatly appreciated!

like image 274
LucidCDN Avatar asked Mar 11 '13 14:03

LucidCDN


People also ask

How will you create an element in HTML form to specify a file to upload?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do I restrict a file type in HTML?

Limiting accepted file types Some examples: accept="image/png" or accept=".png" — Accepts PNG files. accept="image/png, image/jpeg" or accept=".png, .jpg, .jpeg" — Accept PNG or JPEG files.

How do you assign the value of an input type file?

You can't. The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.

What attribute is used to input data into a file?

The input element, having the "file" value in its type attribute, represents a control to select a list of one or more files to be uploaded to the server. When the form is submitted, the selected files are uploaded to the server, along with their name and type.


1 Answers

Try displaying the control first, then set the value, then hide it again:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('image-selector-button').style.display = 'block';");
driver.findElement(By.id("image-selector-button ")).sendKeys("c:\\myFile.bmp");
js.executeScript("document.getElementById('image-selector-button').style.display = 'none';");
like image 92
gilly3 Avatar answered Oct 12 '22 08:10

gilly3