【问题标题】:Selenium multiple upload inputSelenium 多重上传输入
【发布时间】:2023-01-11 03:56:31
【问题描述】:

我知道 selenium 中存在某种错误,导致一次上传多个文件很麻烦(我使用的是 v4.4.0)。当我尝试这样做时,我可以上传第一个文件,但是列表中的第二个文件将与第一个和第三个文件以及第一个和第二个文件一起上传,从而产生第 3x1 个文件、第 2x2 个文件、第 1x3 个文件。

每当我需要上传多个文件时,我都会访问上传位置,上传第一个文件,保存,返回,然后再次访问并上传第二个文件,依此类推。但它在技术上调用了一次上传几次的方法。

现在我在原地,不能选择这样的战术。我读到使用 \n 作为文件字符串的分隔符会有帮助,但它似乎对我不起作用。

如果只发送一个文件(filepaths 是只有一个元素的列表)它工作正常。

    public ApplyFilesPO uploadFile(@NotNull List<String> filepaths) {
        String filepath = String.join("\n", filepaths);
        uploadFilePO.uploadFile(filepath, fileInput, progressBar);
        return this; // doesn't work if list contains more than 1 element
    }

这是UploadFilePO#uploadFile(String, By, By)方法:

    public void uploadFile(String filename, By input, By progressBar) {
        File file = new File(Objects
                .requireNonNull(getClass().getClassLoader().getResource(filename))
                .getFile());
        getExistingElement(input).sendKeys(file.toString());
        waitForElementToDisappear(progressBar);
    }

输入是 2px x 0px 元素,真实用户通过点击某个按钮打开选择文件窗口。

getExistingElement:

    protected final WebElement getExistingElement(By locator) {
        return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
    }

waitForElementToDisappear(上传文件后进度条立即显示,上传后进度条消失,所以我添加了这个方法来确保上传完成:

    protected final <T> void waitForElementToDisappear(T locator) {
        wait.until(isBy(locator)
                ? ExpectedConditions.invisibilityOfElementLocated((By) locator)
                : ExpectedConditions.invisibilityOf((WebElement) locator));
    }

【问题讨论】:

  • 您是否可以手动一次上传多个文件?
  • 一个输入绝对允许,我不确定另一个。
  • 您能否分享方法 upload_File.upload File(filepath, file Input, progressBar) 的代码
  • @SonaliDas 我添加了请求的代码并编辑了问题以添加更多解释。

标签: java selenium file-upload automated-tests


【解决方案1】:

我找到了问题的答案。必须为输入元素提供一个字符串,其中包含要上传的文件的绝对路径,并加上“ ”。

所以这是我现在使用的一段代码:

    public void uploadFiles(@NotNull List<String> filenames, By input, By progressBar) {
        String upload = String.join(" 
 ", filenames
                .stream()
                .map(filename -> new File(Objects.requireNonNull(
                        getClass().getClassLoader().getResource(filename)).getFile()).getAbsolutePath())
                .toList());
        getExistingElement(input).sendKeys(upload);
        wait.until(ExpectedConditions.invisibilityOfAllElements(driver.findElements(progressBar)));
    }

【讨论】:

    【解决方案2】:

    .sendkeys(pathile.jpg ) 解决了?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 2017-09-16
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多