-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Implement page.$eval #638
Implement page.$eval #638
Conversation
This patch: - implements page.$eval and frame.$eval - drops elementHandle.attribute() method in favor of the page.$eval References puppeteer#625
Can this take |
docs/api.md
Outdated
|
||
If `pageFunction` returns a [Promise], then `page.$eval` would wait for the promise to resolve and return it's value. | ||
|
||
Shortcut for [page.mainFrame().$eval(selector, pageFunction)](#frameevalselector-pageFunction). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#frameevalselector-pageFunction -> #frameevalselector-pagefunction (F => f) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Could use an example.. maybe one of these? const searchValue = await page.$eval('#search', el => el.value);
const preloadHref = await page.$eval('link[rel=preload]', el => el.href);
const html = await page.$eval('.main-container', e => e.outerHTML); |
docs/api.md
Outdated
|
||
If `pageFunction` returns a [Promise], then `page.$eval` would wait for the promise to resolve and return it's value. | ||
|
||
Shortcut for [page.mainFrame().$eval(selector, pageFunction)](#frameevalselector-pageFunction). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add some useful examples:
const isHidden = await page.$eval('.hidden', el => el.hasAttribute('hidden'));
const val = await page.$eval('input[type="text"]', el => el.value);
const id = await page.$eval('div.item', el => el.dataset.id);
@JoelEinbinder sure. Do you have an example where arguments would be useful? |
docs/api.md
Outdated
@@ -1056,6 +1076,23 @@ The method queries frame for the selector. If there's no such element within the | |||
|
|||
The method runs `document.querySelectorAll` within the frame. If no elements match the selector, the return value resolve to `[]`. | |||
|
|||
#### frame.$eval(selector, pageFunction) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+ , ...args
?
docs/api.md
Outdated
@@ -31,6 +31,7 @@ | |||
+ [event: 'response'](#event-response) | |||
+ [page.$(selector)](#pageselector) | |||
+ [page.$$(selector)](#pageselector) | |||
+ [page.$eval(selector, pageFunction)](#pageevalselector-pagefunction) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- +
, ...args
? - #pageevalselector-pagefunction-args ?
docs/api.md
Outdated
@@ -94,6 +95,7 @@ | |||
* [class: Frame](#class-frame) | |||
+ [frame.$(selector)](#frameselector) | |||
+ [frame.$$(selector)](#frameselector) | |||
+ [frame.$eval(selector, pageFunction)](#frameevalselector-pagefunction) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- +
, ...args
? - #frameevalselector-pagefunction-args ?
lib/FrameManager.js
Outdated
@@ -197,6 +197,21 @@ class Frame { | |||
|
|||
/** | |||
* @param {string} selector | |||
* @param {function()|string} pageFunction | |||
* @param {!Array<*>} args | |||
* @return {!Promise<?ElementHandle>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not return an element handle. It returns !Promise<*>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
lib/Page.js
Outdated
@@ -148,6 +148,16 @@ class Page extends EventEmitter { | |||
|
|||
/** | |||
* @param {string} selector | |||
* @param {function()|string} pageFunction | |||
* @param {!Array<*>} args | |||
* @return {!Promise<?ElementHandle>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
docs/api.md
Outdated
@@ -323,6 +324,25 @@ The method runs `document.querySelectorAll` within the page. If no elements matc | |||
|
|||
Shortcut for [page.mainFrame().$$(selector)](#frameselector-1). | |||
|
|||
#### page.$eval(selector, pageFunction[, ...args]) | |||
- `selector` <[string]> Selector to query page for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[selector]
This patch:
References #625