@@ -127,12 +127,13 @@ Now, since we're able to use jQuery, let's translate our vanilla JavaScript code
127
127
await page .addScriptTag ({ url: ' https://code.jquery.com/jquery-3.6.0.min.js' });
128
128
129
129
const products = await page .evaluate (() => {
130
- return Array .from ($ (' .product-item' ).map (function () {
130
+ const productCards = $ (' .product-item' );
131
+ return productCards .map (function () {
131
132
const card = $ (this );
132
133
const name = card .find (' .product-item__title' ).text ();
133
134
const price = card .find (' .price' ).contents ().last ().text ();
134
135
return { name, price };
135
- }));
136
+ }). get ( );
136
137
});
137
138
138
139
console .log (products);
@@ -216,12 +217,13 @@ Now, to loop through all of the products, we'll make use of the `$` object and l
216
217
``` js
217
218
const $ = load (await page .content ());
218
219
219
- const products = Array .from ($ (' .product-item' ).map (function () {
220
+ const productCards = $ (' .product-item' );
221
+ const products = productCards .map (function () {
220
222
const card = $ (this );
221
223
const name = card .find (' .product-item__title' ).text ();
222
224
const price = card .find (' .price' ).contents ().last ().text ();
223
225
return { name, price };
224
- }));
226
+ }). get ( );
225
227
226
228
console .log (products);
227
229
```
@@ -244,12 +246,13 @@ await page.goto('https://warehouse-theme-metal.myshopify.com/collections/sales')
244
246
245
247
const $ = load (await page .content ());
246
248
247
- const products = Array .from ($ (' .product-item' ).map (function () {
249
+ const productCards = $ (' .product-item' );
250
+ const products = productCards .map (function () {
248
251
const card = $ (this );
249
252
const name = card .find (' .product-item__title' ).text ();
250
253
const price = card .find (' .price' ).contents ().last ().text ();
251
254
return { name, price };
252
- }));
255
+ }). get ( );
253
256
254
257
console .log (products);
255
258
@@ -270,12 +273,13 @@ await page.goto('https://warehouse-theme-metal.myshopify.com/collections/sales')
270
273
271
274
const $ = load (await page .content ());
272
275
273
- const products = Array .from ($ (' .product-item' ).map (function () {
276
+ const productCards = $ (' .product-item' );
277
+ const products = productCards .map (function () {
274
278
const card = $ (this );
275
279
const name = card .find (' .product-item__title' ).text ();
276
280
const price = card .find (' .price' ).contents ().last ().text ();
277
281
return { name, price };
278
- }));
282
+ }). get ( );
279
283
280
284
console .log (products);
281
285
0 commit comments