我可以在 Puppeteer 的 page.type 上使用 XPath 表达式而不是 CSS 选择器吗?

Can I use XPath expressions instead of CSS selectors on page.type in Puppeteer?

现在的代码:

await page.type('#filterdataTable > div.widget > input', "1234");

我可以使用 XPath 代替这个 CSS 选择器吗?

您可以使用 page.$x() to obtain the ElementHandle 您想要 select 的元素。

然后您可以使用 elementHandle.type()input 字段中键入文本。

const example = await page.$x('//*[@id="filterdataTable"]/div[contains(concat(" ", normalize-space(@class), " "), " widget ")]/input');
await example[0].type('1234');

使用

const [selector] = await scope.context.currentPage.$x(path);
await scope.context.currentPage.evaluate(
    (element, value) => element.value = value, selector, text);

其中 xpath 是他的 XML 路径,text 包含您要键入的字符。