I'm trying to grab products from ebay and open them on amazon. So far, I have them being searched on amazon but I'm struggling with getting the products selected from the search results. Is there a way to select an element based on if the text matches from an array?
Any help greatly appreciated.
const puppeteer = require('puppeteer');
const URL =
https: //www.amazon.co.uk/";
const selectors = {
searchBox: '#twotabsearchtextbox',
productLinks: `//span[@class="a-size-medium a-color-base a-text-normal"]`,
};
(async() => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto('https://www.ebay.co.uk/sch/jmp_supplies/m.html?_trkparms=folent%3Ajmp_supplies%7Cfolenttp%3A1&LH_Complete=1&LH_Sold=1&rt=nc&_trksid=p2046732.m1684');
//Get product titles from ebay
const grabTitles = await page.evaluate(() => {
const itemTitles = document.querySelectorAll('#e1-11 > #ResultSetItems > #ListViewInner > li > .lvtitle > .vip');
var items = []
itemTitles.forEach((tag) => {
items.push(tag.innerText)
})
return items
})
//Search for the products on amazon in a new tab for each product
for (i = 0; i < grabTitles.length; i++) {
const page = await browser.newPage();
await page.goto(URL)
await page.type(selectors.searchBox, grabTitles[i++])
await page.keyboard.press('Enter');
await page.waitForXPath(selectors.productLinks);
const product = await page.$x(selectors.productLinks)
if (product == grabTitles) {
await product.click();
}
}
})()