-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon.js
More file actions
74 lines (56 loc) · 2.11 KB
/
Copy pathamazon.js
File metadata and controls
74 lines (56 loc) · 2.11 KB
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 puppeteer = require('puppeteer');
const path = require('path');
const userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36';
async function newSession(email, password, headless = true, cookies) {
const browser = await puppeteer.launch({
headless: headless,
// userDataDir: path.resolve(__dirname, 'userdata')
});
const page = await browser.newPage();
await page.setUserAgent(userAgent);
if (cookies) {
page.setCookie(...cookies);
}
//await page.goto('https://www.amazon.co.uk/gp/flex/sign-out.html', { waitUntil: 'networkidle2' });
await page.goto('https://www.amazon.co.uk/', { waitUntil: 'networkidle2' });
await Promise.all([
page.click('#nav-link-accountList'),
page.waitForNavigation({ waitUntil: 'networkidle2' }),
]);
await page.waitForSelector('input#ap_email, input[name="email"]');
await page.type('#ap_email', email);
await Promise.all([
page.click('input#continue'),
page.waitForNavigation({ waitUntil: 'networkidle2' }),
]);
await page.waitForSelector('input#ap_password, input[name="password"]');
await page.type('#ap_password', password);
await Promise.all([
page.click('input#signInSubmit'),
page.waitForNavigation({ waitUntil: 'networkidle2' }),
]);
await page.goto('https://www.amazon.co.uk/', { waitUntil: 'networkidle2' });
const allCookies = await page.cookies();
await browser.close();
return allCookies;
}
async function refreshSession(headless = true, cookies) {
const browser = await puppeteer.launch({
headless: headless,
// userDataDir: path.resolve(__dirname, 'userdata')
});
const page = await browser.newPage();
await page.setUserAgent(userAgent);
if (cookies) {
page.setCookie(...cookies);
}
await page.goto('https://www.amazon.co.uk/', { waitUntil: 'networkidle2' });
await Promise.all([
page.click('#nav-link-accountList'),
page.waitForNavigation({ waitUntil: 'networkidle2' }),
]);
const allCookies = await page.cookies();
await browser.close();
return allCookies;
}
module.exports = { newSession, refreshSession };