Hyperion
Hyperion

Reputation: 173

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

The code now:

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

Can I use XPath instead of this CSS Selector?

Upvotes: 7

Views: 8686

Answers (2)

PRACHI RAIKAR
PRACHI RAIKAR

Reputation: 1

Use

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

where xpath is he XML path and text holds the characters you want to type.

Upvotes: 0

Grant Miller
Grant Miller

Reputation: 28999

You can use page.$x() to obtain the ElementHandle of the element you want to select.

Then you can use elementHandle.type() to type text into the input field.

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

Upvotes: 12

Related Questions