I have Javascript puppeteer
code, and PuppeteerSharp for C#
. I know that this libraries are similar, and i know their sites.
But my problem that i barely can manage this libraries, there are alot of methods for each lib, and it hard to find needed methods, even i have working example written on JS.
Please help me rewrite JS code to C#, so it would do similar things. Or at least function names, for example JS (puppeteer) method = C# (puppeteerSharp) method.
(async function main() {
try {
const browser = await puppeteer.launch();
const [page] = await browser.pages();
page.setDefaultTimeout(0);
await page.goto('www.example.com');
await page.waitForSelector('#search-content button.btn-icon');
let count = 0;
while (await page.$('#search-content button.btn-icon') !== null && count != 1) {
const articlesForNow = (await page.$$('#search-content article')).length;
console.log(`Articles for now: ${articlesForNow}. Getting more...`);
count += 1;
await Promise.all([
page.evaluate(
() => {
document.querySelector('#search-content button.btn-icon').click();
}
),
page.waitForFunction(
old => document.querySelectorAll('#search-content article').length > old, {},
articlesForNow
),
]);
}
const articlesAll = (await page.$$('#search-content article')).length;
console.log(`All articles: ${articlesAll}.`);
fs.writeFileSync('full.html', await page.content());
fs.writeFileSync('articles.html', await page.evaluate(
() => document.querySelector('#search-content div.b-filter__inner').outerHTML
));
fs.appendFileSync('articles.txt', await page.evaluate(
(fr) => {
let items = document.querySelectorAll(".product__body");
let appartmentsData = "";
for (let i = 0; i < items.length; i++) {
let itemLink = items[i].querySelector(".product__link").href;
let itemName = items[i].querySelector(".product__link strong").innerHTML;
let itemPrice = items[i].querySelector(".product__value").innerHTML;
return appartmentsData;
}, fr
));
// rest of the code
That what I have so far:
using(var browser = await Puppeteer.LaunchAsync(new LaunchOptions())) {
var page = await browser.NewPageAsync();
await page.GoToAsync(LINK);
await page.WaitForSelectorAsync("#search-content button.btn-icon");
while (await page.QuerySelectorAsync("#search-content button.btn-icon") != null) {
var articlesForNow = await page.QuerySelectorAllAsync("#search-content article");
Console.WriteLine("Items proceed: " + articlesForNow.Length);
for (int i = 0; i < articlesForNow.Length; i++) {
string itemOuterHtml = await articlesForNow[i].EvaluateFunctionAsync < string > ("e => e.outerHTML");
}
await page.WaitForSelectorAsync("#search-content button.btn-icon").EvaluateFunctionAsync("e => e.click()");
}
}
But It's infinity counting and does not stop. After element is 1275 it throws the error about my method in while loop.
PuppeteerSharp.WaitTaskTimeoutException: waiting for selector '#search-content button.btn-icon' failed: timeout 30000ms exceeded