0

The goal is to launch the html page, enter a url from booking.com, click the button, and have the scraped hotel name, rating, etc returned in the console.

So far, it does not return anything when clicking the button. It works when the URL is hard-coded, but It says "main is declared but value is never read" in this form. Am i calling the function incorrectly? I'm still new to puppeteer, perhaps I'm overlooking something?

Here is app.js

function main()
{
    var Url = document.getElementById('inputUrl').value

    const puppeteer = require('puppeteer');

    let bookingUrl = Url;
    (async () => {
        const browser = await puppeteer.launch({ headless: true });
        const page = await browser.newPage();
        await page.goto(bookingUrl);

        // get hotel details
        let hotelData = await page.evaluate(() => {
            let hotels = [];
            // get the hotel elements
            let hotelsElms = document.querySelectorAll('div.sr_property_block[data-hotelid]');
            // get the hotel data
            hotelsElms.forEach((hotelelement) => {
                let hotelJson = {};
                try {
                        hotelJson.name = hotelelement.querySelector('span.sr-hotel__name').innerText;
                        hotelJson.reviews = hotelelement.querySelector('div.bui-review-score__text').innerText;
                        hotelJson.rating = hotelelement.querySelector('div.bui-review-score__badge').innerText;
                        if(hotelelement.querySelector('div.bui-price-display__value.prco-inline-block-maker-helper'))
                        {
                            hotelJson.price = hotelelement.querySelector('div.bui-price-display__value.prco-inline-block-maker-helper').innerText;
                        }
                        hotelJson.imgUrl = hotelelement.querySelector('img.hotel_image').attributes.src.textContent;
                    }
                    catch (exception){

                    }
                hotels.push(hotelJson);
            });
            return hotels;
        });
        console.dir(hotelData);
    })();
}

Here is index.html

<!DOCTYPE html>

 <html lang="en">
    <head>
        <meta charset="utf-8">
        <script src="app.js" type="text/javascript"></script> 
        <title></title>
        <meta name="description" content="">
        <link rel="stylesheet" href="">
    </head>

    <input id = "inputUrl" type="text" placeholder = "type url here"/>
    <button id = "button" button onclick="main();"> click</button>

    <body>

        <script src="" async defer></script>
    </body>
</html>

hardkoded
  • 18,915
  • 3
  • 52
  • 64
IscA
  • 59
  • 8
  • Are you trying to [run Puppeteer in the browser](https://stackoverflow.com/questions/54647694/how-to-run-puppeteer-code-in-any-web-browser)? I think you've overlooked that Puppeteer only runs on Node.js. – ggorlen Aug 22 '23 at 03:14

1 Answers1

0

You could add this before the evaluate:

await page.waitForSelector('div.sr_property_block[data-hotelid]');
hardkoded
  • 18,915
  • 3
  • 52
  • 64