[英]How to upload a file through the js-doc-upload button, using Selenium WD + Java + JUnit?
I want to upload a file and code of upload button is like: 我要上传文件,并且上传按钮的代码如下:
<button class="panel-box-header__controls-bttn bttn-icon js-doc-upload" type="button">
<svg class="icon icon-clip">
<use xlink:href="#icon-clip"></use>
</svg>
</button>
Method driver.findElement(By.xpath("...")).sendKeys("path of the file which u want to upload");
方法
driver.findElement(By.xpath("...")).sendKeys("path of the file which u want to upload");
doesn't work here 在这里不工作
Method Robot also doesn't work: 方法机器人也不起作用:
public void upload() throws Exception { uploadButton.click(); Thread.sleep(2000); //File Need to be imported File file = new File("/Users/admin/Desktop/test_image.jpg"); StringSelection stringSelection= new StringSelection(file.getAbsolutePath()); //Copy to clipboard Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); Robot robot = new Robot(); // Cmd + Tab is needed since it launches a Java app and the browser looses focus robot.keyPress(KeyEvent.VK_META); robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_META); robot.keyRelease(KeyEvent.VK_TAB); robot.delay(500); //Open Goto window robot.keyPress(KeyEvent.VK_META); robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_META); robot.keyRelease(KeyEvent.VK_SHIFT); robot.keyRelease(KeyEvent.VK_G); //Paste the clipboard value robot.keyPress(KeyEvent.VK_META); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_META); robot.keyRelease(KeyEvent.VK_V); //Press Enter key to close the Goto window and Upload window robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); robot.delay(500); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); }
When I use the Robot method as in the example above, there is a click on the upload button, then a window with files opens and nothing happens anymore, the specified file isn't upload. 当我在上面的示例中使用Robot方法时,单击了上载按钮,然后将打开一个包含文件的窗口,并且不再执行任何操作,指定的文件没有上载。
Maybe someone has a solution to this problem? 也许有人可以解决这个问题? Using the Robot method or any other options
使用机器人方法或任何其他选项
You can't .sendKeys()
to a button or svg element. 您不能
.sendKeys()
到按钮或svg元素。 Neither are designed to accept input. 两者都不旨在接受输入。
The .sendKeys()
method works when the site uses a standard <input type="file />
input for upload. 当站点使用标准
<input type="file />
输入进行上载时, .sendKeys()
方法起作用。
You'll need to use Robot or some other method to click on the button and select the file for upload. 您需要使用漫游器或其他方法单击该按钮,然后选择要上传的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.