-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
63 lines (52 loc) · 1.6 KB
/
test.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
'use strict'
const puppeteer = require('puppeteer')
const Config = require('./config')
const devices = require('puppeteer/DeviceDescriptors')
// const iPhone6 = devices['iPhone 6']
;(async() => {
const browser = await puppeteer.launch({
//设置超时时间
timeout: 15000,
//如果是访问https页面 此属性会忽略https错误
ignoreHTTPSErrors: true,
// 打开开发者工具, 当此值为true时, headless总为false
devtools: false,
// 关闭headless模式, 会打开浏览器
headless: false
})
const page = await browser.newPage()
// 模拟 iPhone6
// await page.emulate(iPhone6)
await page.goto(Config.testUrl)
// 获取页面标题
let title = await page.title()
console.log('页面标题为:' + title)
// 获取指定dom内容
const SELECTOR_PRODUCT_NUM= '#setf'
const productNum = await page.$eval(SELECTOR_PRODUCT_NUM, el =>{
return el.innerText
})
console.log('获取指定dom内容:' + productNum)
// 循环数据
const SELECTOR_PRODUCT_INFO= '#lh a'
const productInfo = await page.$$eval(SELECTOR_PRODUCT_INFO, el =>{
return el.map(v => {
const name = v.innerText
return {
name
}
})
})
console.log('循环数据1为:' + productInfo[0].name)
// 从某个dom下根据选择器获取dom
const dom1 = await page.evaluate(()=>{
const SELECTOR_PRODUCT_dom1= '#cp'
let el = document.querySelectorAll(SELECTOR_PRODUCT_dom1)[0]
return el.querySelector('#jgwab').innerText
})
console.log('#cp > #jgwab的内容为:' + dom1)
await page.screenshot({
path: 'screenshot/demo.png'
})
// await browser.close()
})()