I have an array, one would return an invalid search result on the website, the other will return a valid search.
["sakdjlkasjda", "Assassin's Creed Origins"]
I then map over the array and pass the value to an async function
const cex = games.map((game) => cexSearch(game));
return Promise.all(cex)
.then(function(g) {
console.log(g);
res.send(g);
});
In the async function, I create a Puppeteer instance, navigate to the URL. The website has an element (without a class or id) that is only displayed where there are no results. For valid results noRecordsDisplay should equal none, where there are no valid results noRecordsDisplay should equal "". However, a few times I've noticed that for a search that should be invalid, noRecordsDisplay equals none, so not sure where I am going wrong here that it works most of the time, but not all the time? Any help would be greatly appreciated.
async function cexSearch(game) {
const url = 'https://uk.webuy.com/search?stext=' + game;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
await page.goto(url, {
timeout: 3000000
});
const content = '.content-area';
await page.waitForSelector(content);
await page.waitForSelector('.content-area > div:not(.searchRcrd)');
const noRecordsDisplay = await page.evaluate(() => document.querySelector('.content-area > div:not(.searchRcrd)').style.display);
console.log("display = " + noRecordsDisplay);
if (noRecordsDisplay === "") {
return "No Search Results";
} else {
//When there is an invalid search it sometimes reaches here and .searchRcrd does not exist so it timesout
const selector = '.searchRcrd';
await page.waitForSelector(selector);
// DO logic
await browser.close();
return records;
}
}