【问题标题】:How to upload a file through the js-doc-upload button, using Selenium WD + Java + JUnit?如何使用 Selenium WD + Java + JUnit 通过 js-doc-upload 按钮上传文件?
【发布时间】:2018-05-14 15:07:50
【问题描述】:

我要上传一个文件,上传按钮的代码如下:

<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>

方法driver.findElement(By.xpath("...")).sendKeys("path of the file which u want to upload"); 在这里不起作用

Method Robot 也不起作用:

    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);
    }

当我使用上面示例中的 Robot 方法时,单击上传按钮,然后打开一个带有文件的窗口,并且没有任何反应,指定的文件没有上传。

也许有人可以解决这个问题?使用 Robot 方法或任何其他选项

【问题讨论】:

    标签: java selenium-webdriver file-upload junit automated-tests


    【解决方案1】:

    您不能 .sendKeys() 到按钮或 svg 元素。两者都不是为了接受输入而设计的。

    当站点使用标准&lt;input type="file /&gt; 输入进行上传时,.sendKeys() 方法有效。

    您需要使用机器人或其他方法单击按钮并选择要上传的文件。

    【讨论】:

    • 是的,我知道我不能使用.sendKeys(),我只需点击上传按钮(通过xpath),打开带有文件的框架后,我不知道我可以使用哪种方法上传一个文件。我见过一些使用机器人方法的例子,但无论如何它对这个上传器没有帮助。好吧,我还是会搜索更多选项,谢谢您的回答
    猜你喜欢
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2015-07-09
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2015-12-11
    相关资源
    最近更新 更多