0

我在 pupeteer 中有一个页面,我正在尝试启动文件上传,它必须上传按钮。问题是启动文件上传的两个按钮具有相同的类型和选择器。此工作代码允许我完美地将文件上传到第一个文件输入。

input = await mainpage.$('input[type="file"]');
     await input.uploadFile(`${__dirname}/images/${photoID}.png`);
     await mainpage.waitFor(500);

现在我的问题是 - 有没有办法让我以编程方式在 pupeteer 的页面上定位类型文件的 SECOND 输入?因为它们都具有相同的文件选择器ID等输入类型。我可以在相同的代码中执行某种索引吗?我尝试了所有其他方法,由于选择器是相同的,这是我让第一个在页面上工作的唯一方法。

4

1 回答 1

1

Use the page.$$ API to fetch the list of all matching elements. As the docs mention, it returns an array of matching elements and then you should be able to index the array. Somtehing like:

// Get all the elements for the selector
inputs = await mainpage.$$('input[type="file"]');

// Work on individual elements
await input[0].uploadFile(`${__dirname}/images/${photoID}.png`);
...
于 2020-12-24T06:21:12.933 回答