-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.js
73 lines (68 loc) · 1.85 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { logger, screen } = require("./utils");
const moment = require("moment");
const searchURL = "https://grocery.walmart.com/search/?query=";
const $items = `[id^="item-"]`;
const $addtocart = `[data-automation-id="addToCartBtn"]`;
const $itemname = '[data-automation-id="name"]';
const $itemimage = '[data-automation-id="image"]';
const $itempricedollar = '[data-automation-id="wholeUnits"]';
const $itempricecents = '[data-automation-id="partialUnits"]';
const searchProduct = async (
page,
query,
shouldClick = false,
shouldShowImage = true
) => {
const url = await page.url();
const searchQuery = searchURL + encodeURIComponent(query);
if (!url || url !== searchQuery) {
logger("navigating to search page");
await page.goto(searchQuery);
} else {
logger("Already on search page");
}
await page.waitForSelector($items);
logger("found items");
const [first] = await page.$$($items);
if (!first) {
logger("cant find 1 item");
return;
}
const button = await first.$($addtocart);
// scrape item information
const title = await first.$eval($itemname, element => element.innerHTML);
logger("found title");
let image = null;
if (shouldShowImage) {
image = await first.$eval($itemimage, element => element.src);
logger("found image");
}
const dollars = await first.$eval(
$itempricedollar,
element => element.innerHTML
);
const cents = await first.$eval(
$itempricecents,
element => element.innerHTML
);
logger("found price");
// click button
if (shouldClick) {
if (!button) {
logger("cant click button, maybe you already added it to the cart");
} else {
await button.click();
logger("clicked add to cart");
}
}
await screen(page, moment().format("X"));
return {
image,
cents,
title,
dollars
};
};
module.exports = {
searchProduct
};