Skip to content

Instantly share code, notes, and snippets.

@RoyiNamir
Created November 14, 2018 10:22
Show Gist options
  • Save RoyiNamir/8610fbbecbef9387810e90f292282646 to your computer and use it in GitHub Desktop.
Save RoyiNamir/8610fbbecbef9387810e90f292282646 to your computer and use it in GitHub Desktop.
import puppeteer, {ElementHandle} from "puppeteer";
const getPropAsync = async (elementHandle: ElementHandle, propertyName: string) =>
{
let href = await elementHandle.getProperty(propertyName);
return await (await elementHandle.getProperty(propertyName)).jsonValue();
};
const getElementsAsync = (async (selector: string, section: ElementHandle | null, propertyName: string | null, page) =>
{
let cubes: ElementHandle[] = [];
if (section)
{
cubes = await section.$$(selector);
}
else
{
cubes = await page.$$(selector);
}
if (propertyName)
{
return await Promise.all(cubes.map(a => getPropAsync(a, propertyName)));
}
else
{
return cubes;
}
});
(async () =>
{
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
let url = "https://www.mutualart.com/Artists";
console.log(`Fetching page data for : ${url}...`);
await page.goto(url);
await page.waitForSelector(".item.col-xs-3");
// let arrMainLinks: ElementHandle[] = await page.$$('.item.col-xs-3 > a');
let arrMainLinks = await page.evaluate(() =>
{
// click the first element
return Array.from(document.querySelectorAll('.item.col-xs-3 > a'));
});
console.log(arrMainLinks.length);
for (let i = 0; i < arrMainLinks.length; i++) //get the main links
{
// const mainLink = arrMainLinks[i];
// let hrefValue =await (await mainLink.getProperty('href')).jsonValue();
//console.log("Clicking on " + hrefValue);
/* await Promise.all([
mainLink.click({delay: 100})
]);*/
await page.evaluate((a) =>
{
// click the first element
return ([...document.querySelectorAll('.item.col-xs-3 > a')][a] as HTMLElement ).click();
}, i);
await page.waitForNavigation();
//gallery
// let arrSubLinks: ElementHandle[] = await page.$$('.slide>a');// >a>.slide-content
let arrSubLinks2 = await page.evaluate(() =>
{
// click the first element
return Array.from(document.querySelectorAll('.slide>a'));
});
console.log(arrSubLinks2.length);
for (let j = 0; j < arrSubLinks2.length; j++)
//let's click on each sub click
{
console.log('███AAA');
// await arrSubLinks[j].click();
await page.evaluate((a) =>
{
// click the first element
return ([...document.querySelectorAll('.slide>a')][a] as HTMLElement) .click();
}, j);
// const childBtn = await arrSubLinks[i].$('a.img-block');
// await childBtn.click();
// const sublink = arrSubLinks[j];
// page.click('div.selectize-input.items.not-full.has-options')
// sublink.click({delay: 100})
// await page.waitFor(7000)
/* await Promise.all([
page.waitForNavigation({waitUntil: 'networkidle2'}),
]);*/
await page.waitForNavigation();
let ddd: ElementHandle[] = await page.$$('.artist-name');
console.log(ddd.length);
/* await Promise.all([
page.waitForNavigation(),
sublink.click({delay: 100})
]);*/
console.log('███BBB');
await page.waitFor(2000);
await page.goBack();
console.log('███CCC');
// break;
}
await page.waitFor(2000);
await page.goBack();
}
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment