-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (116 loc) · 4.23 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const cheerio = require('cheerio')
const tools = require('os-tools')
const self = module.exports = {
/**
* will return the dom as a cheerio element
* @returns {Promise<*>}
*/
getCheerioDom: async function (page) {
const eleHandle = await page.$('body');
const innerHTML = await page.evaluate(body => body.innerHTML, eleHandle);
const $ = cheerio.load(innerHTML)
require('cheerio-get-css-selector').init($);
return $
},
/**
* will return all of the child nodes of a given cheerio element
*/
getChildNodes: function (cheerioEle) {
return cheerioEle.contents()
},
/**
* will return a specific node from nodes list
*/
getElementAt: function(cheerioChildrenEle, idx){
return cheerioChildrenEle.eq(idx)
},
/**
* will return the :th child node
*/
getChildAt: async function(parentEle, childAtIdx){
return (await self.getChildNodes(parentEle)).eq(childAtIdx)
},
/**
* will return an element from the cheerio dom
*/
getElementFromDom: function (cheerioDom, selector) {
return cheerioDom(selector)
},/**
* will return an element from a cheerio element
*/
getElements: function (cheerioElement, selector) {
return cheerioElement.find(selector)
},
/**
* will return an element from the dom
*/
getElementByText: function (cheerioEle, text) {
return cheerioEle.find(":contains('"+text+"')")
},
/**
* will return the attrib value of an element
*/
getAttributeValueFromCheerioEle: function (cheerioEle, attrib) {
return cheerioEle.attr(attrib)
},
/**
* will wait for an element to change it's value
*/
waitForSelectorToChangeAttValue: async function (page, selector, att, val, checkEach = 3000) {
while (true) {
try {
let cheerioDom = await self.getCheerioDom(page)
await tools.delay(checkEach)
let ele = await self.getElementFromDom(cheerioDom, selector)
let eleVal = await self.getAttributeValueFromCheerioEle(ele, att)
if (eleVal === val)
return
} catch (e) {
}
}
},
/**
* Will loop on a <select> element children (<option> tags) and look for a given text.
* Optionally: if the text exists, will select it.
* NOTICE: if you want to select the option, you need to supply a puppeteer helper instance,
* a page instance, and toggle the selectIfExists to true
* @param selectCheerioEle -> cheerio element. Scrape the dom first with getCheerioDom(page)
* then with getElementFromDom(selector) find your element
* @param reqText -> the text of the option to look for
* @param selectIfExists -> true to select
* @param page -> puppeteer page, if you want to select the option
* @param ph -> optional PuppeteerHelper.js instance you created, if you want to select the option
* @param delayAfterSelect -> if you chose
*/
isSelectHasValue: async function (selectCheerioEle,
reqText,
selectIfExists = false,
ph = null,
page = null,
delayAfterSelect = 0) {
let options = await self.getChildNodes(selectCheerioEle)
for(let i = 0; i < options.length; i++){
let currEle = options.eq(i)
if (currEle.text() === reqText) {
if (selectIfExists) {
let reqValue = await self.getAttributeValueFromCheerioEle(currEle, "value")
await ph.selectByValue(page, self.getUniqueSelector(selectCheerioEle), reqValue,delayAfterSelect)
}
return true
}
}
return false
},
/**
* will return a unique selector for the cheerio element
*/
getUniqueSelector: function (cheerioEle) {
return cheerioEle.getUniqueSelector()
},
/**
* will return the text from an element
*/
getTextFromElement: function (cheerioEle) {
return cheerioEle.text()
}
};