0

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
Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73

1 Answers1

4

We cannot convert the whole code for you but here are some pointers. You need to take one chunk of problem at a time.

Break the while loop

Let us look into the JS code,

let count = 0;
while (await page.$('#search-content button.btn-icon') !== null && count != 1) {}

It's creating a while look, which stops if count is 1.

Now your C# Code,

while (await page.QuerySelectorAsync("#search-content button.btn-icon") != null)

It's not checking for count, this will end up in infinite while loop.

You should count the number,

int count = 0;
while (await page.QuerySelectorAsync("#search-content button.btn-icon") != null && count != 1){
 // other code
 count++;
}

This way it will stop after it finds one result.

Learn more about Promise.all etc.

Your rest of the question is about Promise.all and few other stuff. Here are some useful links,

Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73