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

Implement page.$eval #638

Merged
merged 5 commits into from
Aug 31, 2017
Merged

Implement page.$eval #638

merged 5 commits into from
Aug 31, 2017

Conversation

aslushnikov
Copy link
Contributor

This patch:

  • implements page.$eval and frame.$eval
  • drops elementHandle.attribute() method in favor of the page.$eval

References #625

This patch:
- implements page.$eval and frame.$eval
- drops elementHandle.attribute() method in favor of the page.$eval

References puppeteer#625
@JoelEinbinder
Copy link
Contributor

Can this take ...args like the old page.$?

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).
Copy link
Contributor

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) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@paulirish
Copy link
Contributor

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).
Copy link
Contributor

@ebidel ebidel Aug 31, 2017

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);

@aslushnikov
Copy link
Contributor Author

@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)
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Aug 31, 2017

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. + , ...args ?
  2. #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)
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. + , ...args ?
  2. #frameevalselector-pagefunction-args ?

@@ -197,6 +197,21 @@ class Frame {

/**
* @param {string} selector
* @param {function()|string} pageFunction
* @param {!Array<*>} args
* @return {!Promise<?ElementHandle>}
Copy link
Contributor

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<*>

Copy link
Contributor Author

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>}
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[selector]

@aslushnikov aslushnikov merged commit 62ececb into puppeteer:master Aug 31, 2017
@aslushnikov aslushnikov deleted the deval branch October 10, 2017 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants