如何在不使用autoit且仅使用selenium和java的情况下上传文件?

问题描述 投票:0回答:2

这是文件上传无效的按钮检查元素

<button class="btn btn-success text-capitalize" id="ac-btn-imprt" type="button">Browse File</button>

因此,当我尝试执行sendkeys时,它不起作用,因为对于发送键,它必须是我的情况下的输入标记,它是type = button。

提前致谢。

注意1.我使用selenium和java来自动化我的应用程序。 2.我使用了间歇性工作的机器人类,我无法调试机器人类实现,因为我还没有使用它。

java selenium ui-automation browser-automation
2个回答
0
投票

您可以使用ROBOT API jar来上传文件。只需触发浏览按钮,当您需要提供要上传的文件的位置时,使用Robot API发送位置,然后再使用Robot API按回车键。

try {
        //Setting clipboard with file location
        setClipboardData(fileLocation);
        //native key strokes for CTRL, V and ENTER keys
        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } catch (Exception exp) {
        exp.printStackTrace();
    }

0
投票

如果使用RemoteWebDriver,请将LocalFileDetector添加到驱动程序:

driver.setFileDetector(new LocalFileDetector());

在HTML中查找隐藏的<input type="file">并将sendKeys绝对路径发送到文件。

你可以找到的细节:

How to upload file using Selenium WebDriver in Java

https://sqa.stackexchange.com/questions/12851/how-can-i-work-with-file-uploads-during-a-webdriver-test

© www.soinside.com 2019 - 2024. All rights reserved.