简体   繁体   English

如何使用Selenium WD + Java + JUnit通过js-doc-upload按钮上传文件?

[英]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.

相关问题 如何使用Junit Selenium上传文件? - How to upload the file using Junit Selenium? 如何使用Selenium Webdriver中的“上传图像按钮”上传文件/照片 - How to Upload File/Photo using “Upload Image Button” in Selenium Webdriver 如何使用 selenium java 上传文件 - How to upload file using selenium java 如何在Java中使用Selenium WebDriver上传文件 - How to upload file using Selenium WebDriver in Java 无法使用 Selenium Java 中的 sendKeys() 将文件上传到“浏览”按钮 - Unable to upload a file to the "browse" button using sendKeys() in Selenium Java 如何使用Selenium和Java通过类型为“ hidden”的“ input”元素上传文件? - How to upload file through an 'input' element with type=“hidden” using Selenium and Java? 如何在不使用autoit且仅使用selenium和java的情况下上传文件? - How to upload a file without using autoit and with only using selenium and java? 在Mac上使用Selenium WebDriver和Java进行文件上载 - File Upload Using Selenium WebDriver and Java on a Mac 如何使用Selenium Java中的机器人类在无头浏览器中上传文件 - How to upload file in headless browser using robot class in selenium java 如何使用Selenium的Java接口从站点上载文件 - How to upload a file from a site using Selenium's java inteface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM