detect no job after last page load - restart browser after this happen

This commit is contained in:
bossgeekgo
2024-02-09 17:41:13 +08:00
parent f1b418a78e
commit 21119e346b
+17 -4
View File
@@ -75,7 +75,7 @@ async function mainLoop () {
);
await sleepWithRandomDelay(2000)
const { targetJobElProxy, targetJobIndex } = await new Promise(async (resolve) => {
const { targetJobElProxy, targetJobIndex } = await new Promise(async (resolve, reject) => {
// job list
const recommendJobListElProxy = await page.$('.job-list-container .rec-job-list')
@@ -85,7 +85,9 @@ async function mainLoop () {
`
)
let targetJobIndex = jobListData.findIndex(it => [...expectCompanySet].find(name => it.brandName.includes(name)))
while (targetJobIndex < 0) {
let hasReachLastPage = false
while (targetJobIndex < 0 && !hasReachLastPage) {
// fetch new
const recommendJobListElBBox = await recommendJobListElProxy.boundingBox()
const windowInnerHeight = await page.evaluate('window.innerHeight')
@@ -101,6 +103,12 @@ async function mainLoop () {
await page.mouse.wheel({deltaY: increase});
await sleep(1)
}
hasReachLastPage = await page.evaluate(`
!(document.querySelector('.job-recommend-main')?.__vue__?.hasMore)
`)
if (hasReachLastPage) {
console.log(`Arrive the terminal of the job list.`)
}
await sleep(3000)
jobListData = await page.evaluate(
@@ -111,6 +119,12 @@ async function mainLoop () {
targetJobIndex = targetJobIndex = jobListData.findIndex(it => [...expectCompanySet].find(name => it.brandName.includes(name)))
}
if (targetJobIndex < 0 && hasReachLastPage) {
// has reach last page and not find target job
reject(new Error('CANNOT_FIND_EXCEPT_JOB'))
return
}
const recommendJobItemList = await recommendJobListElProxy.$$('ul.rec-job-list > li')
resolve(
{
@@ -144,8 +158,6 @@ async function mainLoop () {
}
);
await sleepWithRandomDelay(2000)
}
const jobData = await page.evaluate('document.querySelector(".job-detail-box").__vue__.data')
const startChatButtonInnerHTML = await page.evaluate('document.querySelector(".job-detail-box .op-btn.op-btn-chat")?.innerHTML.trim()')
@@ -193,6 +205,7 @@ async function mainLoop () {
} else {
}
}
}
// ;await browser.close()
} catch (err) {