Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the right way of clicking an ElementHandle<Node> since v15.3.1 #8852

Closed
Qlub53 opened this issue Aug 28, 2022 · 4 comments
Closed

What is the right way of clicking an ElementHandle<Node> since v15.3.1 #8852

Qlub53 opened this issue Aug 28, 2022 · 4 comments
Assignees

Comments

@Qlub53
Copy link

Qlub53 commented Aug 28, 2022

I used to use page.$x(expression) and elementHandle.click([options]) with puppeteer v13.4 just fine.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const handles = await page.$x('xpath');
await handles[0].click();

But now since the API signatures have changed ($x(expression) and click(this, options)), the above usage ends up with the following tsc compilation error:

error TS2684: The 'this' context of type 'ElementHandle<Node>' is not assignable to method's 'this' of type 'ElementHandle<Element>'.

7 await handles[0].click();
        ~~~~~~~~~~

How shall I transit from using Element to using Node?

Originally posted by @Qlub53 in #8552 (comment)

@Qlub53 Qlub53 changed the title What is the right way of clicking an ElementHandle<Node> since v15.3.1 What is the right way of clicking an ElementHandle<Node> since v15.3.1 Aug 28, 2022
@OrKoN
Copy link
Collaborator

OrKoN commented Aug 29, 2022

I think this is expected since XPath selectors might return not only elements. Try the following: await (handles[0] as ElementHandle<Element>).click(); if the selector is guaranteed to contain an element.

@Qlub53
Copy link
Author

Qlub53 commented Aug 29, 2022

Yeah, I circumvented it with type casting, too.
But having to type cast every single time makes the code really unpleasant to read.

Maybe this is one of few occasions where javascript is more attractive than typescript. 😂

@OrKoN
Copy link
Collaborator

OrKoN commented Aug 30, 2022

@jrandolf What do you think? Can we do something to be type-correct but also make it easier for xpath users? e.g., maybe infer the type from the selector similar to what we do for CSS selectors?

@jrandolf
Copy link
Contributor

There are no plans to fix this anytime soon. See #8579 for potential alternatives being discussed.

@jrandolf jrandolf closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2022
addie-tyc added a commit to addie-tyc/shopee-coins-collector that referenced this issue Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants