使用WebDriver,如果元素是type ="file",我通常可以直接将.sendKeys()直接添加到元素中,并且它可以很好地工作.
不幸的是,我现在的情况是使用了一个自定义按钮,它有一个引用输入元素的锚标记:
//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"}}/>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,sendKeys()不适用于锚标记:
driver.findElement(By.id("image-upload-button ")).sendKeys(“c:\\myFile.bmp”);
Run Code Online (Sandbox Code Playgroud)
错误:org.openqa.selenium.WebDriverException:focusElement执行失败; 无法发送密钥,因为无法聚焦元素
sendKeys()也不工作不可见的文件输入元素:
driver.findElement(By.id("image-selector-button ")).sendKeys(“c:\\myFile.bmp”);
Run Code Online (Sandbox Code Playgroud)
错误:org.openqa.selenium.ElementNotVisibleException:必须显示元素才能单击
我尝试过注入javaScript以几种不同的方式在不可见的输入元素上设置路径值(见下文),但似乎没有任何效果.关于如何设置这条路径的任何想法?
//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 …Run Code Online (Sandbox Code Playgroud)