admin管理员组文章数量:1406018
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!
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!
Share Improve this question edited Mar 11, 2013 at 17:13 LucidCDN asked Mar 11, 2013 at 14:55 LucidCDNLucidCDN 1131 silver badge5 bronze badges 1- Not sure why this was closed, it's a very clear question if you have worked with Selenium WebDriver and a very good answer as well. – Ardesco Commented Mar 13, 2013 at 6:33
1 Answer
Reset to default 4Try 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';");
本文标签:
版权声明:本文标题:javascript - How can I set a path for a file to upload if an <input> element is not visible and only reference by 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965906a2634944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论