redochka
redochka

Reputation: 12819

puppeteer form submit: evaluate is not a function

the form exists in the page and i am sure.

const form = await page.$('#my-form');
await form.evaluate(form => form.submit());

I get this error:

TypeError: form.evaluate is not a function

Upvotes: 5

Views: 3090

Answers (3)

ggorlen
ggorlen

Reputation: 56885

the form exists in the page and i am sure

How are you sure? If you're verifying the form's presence by looking at the developer tools, that's no guarantee that the element wasn't added dynamically via JS after the page.$ call runs. view-source: is the place to look for statically-present elements (and when JS runs, some of those elements may be changed or removed, so it's also no guarantee of an element's presence once JS starts executing).

page.$ does not wait for the element to appear, so if you're using it immediately after a navigation, it may well fail. The solution is typically page.waitForSelector. In your case,

const form = await page.waitForSelector('#my-form');
await form.evaluate(form => form.submit());

If this times out, then there may be a typo in your selector, the element may be in an iframe or shadow root, or the page may be blocking you. Without a reproducible example, it's anyone's guess.

Upvotes: 0

Moshisho
Moshisho

Reputation: 2961

For new comers, if you encounter this problem. You're probably using puppeteer 1.19 or lower and need to update npm update puppeteer. Use the API of your version (see links at the top of the page by version).

Upvotes: 1

Md. Abu Taher
Md. Abu Taher

Reputation: 18826

EDIT 2019: As mentioned by Kyle, the latest puppeteer have .evaluate method on elementHandle. It's been two years after all.

const tweetHandle = await page.$('.tweet .retweets');
expect(await tweetHandle.evaluate(node => node.innerText)).toBe('10');

You can try it this way,

await page.evaluate(() => {
 const element = document.querySelector("#my-form")
 element.submit()
});

ElementHandle does not have a .evaluate function property. Check the docs.

Upvotes: 1

Related Questions