如何在无头模式下使用按钮标签上传文件

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

我正在尝试以无头模式运行文件上传,我尝试了不同的方法,但是没有用。如何以无头模式上传文件?

  1. DOM中的导入按钮如下:

enter image description here

这不是输入标签,我不确定这是否会影响结果?

  1. 我需要它以无头模式运行。

以下是我尝试过的所有方法:

  1. 使用WebUI.uploadFile关键字

在默认模式和无头模式下不起作用。

  1. 定义自定义关键字,如下所示:
    public class MyTools {

        @Keyword
        def uploadFile (TestObject to, String filePath) {
            WebUI.click(to)
            WebUI.delay(2)
            StringSelection ss = new StringSelection(filePath);
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.delay(1000)
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.delay(1000) //NOTE THE DELAY (500, 1000, 1500 MIGHT WORK FOR YOU)
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        }
    }

这种方式可以在默认的Chrome模式下运行,但在无头模式下总是会失败。

  1. 使用以下代码:
    WebDriver driver = DriverFactory.getWebDriver()

    String path = 'D:\\Daily task\\New PW User.csv'

    driver.findElement(By.xpath("//*[@id='app']/div/section/div/section[1]/div[1]/div/button")).sendKeys(path);

在默认模式和无头模式下均不起作用。

有人可以帮我吗?

file-upload katalon-studio
1个回答
0
投票

机器人类无法在无头模式下工作,至少是not that simple

此外,我认为您不需要它(不清楚您为什么在示例中需要它)。尝试将uploadFile()方法更改为此:

    @Keyword
    def uploadFile (TestObject to, String filePath) {
        WebUI.click(to)
        WebUI.delay(2)
        WebElement element = WebUiCommonHelper.findWebElement(to, 30)
        element.sendKeys(filePath)
    }

注意:

您将需要导入com.kms.katalon.core.webui.common.WebUiCommonHelper才能将测试对象转换为Web元素。

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