Selecting title and address from Google maps search result link using Puppeteer

Solution for Selecting title and address from Google maps search result link using Puppeteer
is Given Below:

I would like to console log “shop name” and “shop address” from a google maps list. I searched “flower shop chicago” on google maps and clicked one of the links. Then, I tried to console log shop name and address. I having a hard time using querySelector (css selector) to console log what I need. Also, when I copy and paste long Google Maps link for a shop (https://www.google.com/maps/place/Donna’s+Garden+Flower+Shop+-+Chicago,+IL/@41.9898102,-87.7360212,17z/data=!3m1!4b1!4m5!3m4!1s0x880fce639267ed2f:0x3e47d8ddf3040316!8m2!3d41.9898313!4d-87.7338812?authuser=0&hl=en) , it gives an error but it works on browser. It does not give an error if I shorten it.

I will be happy if you can check my code. Thanks

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto('https://www.google.com/maps/place/Donna's+Garden+Flower+Shop+-+Chicago,+IL/@41.9898102,-87.7360212,17z/data=!3m1!4b1!4m5!3m4!1s0x880fce639267ed2f:0x3e47d8ddf3040316!8m2!3d41.9898313!4d-87.7338812?authuser=0&hl=en');
  //await page.screenshot({ path: 'example.png' });

 const shopName = await page.$eval("x3AX1-LfntMc-header-title-title span", span => span.textContent);
 console.log(shopName);

  //await browser.close();
})();
  1. If a URL contains a single quote ' you need to use double qoutes " or backticks ` for it.

  2. You need a dot before class names in selectors.

  3. In your case, it seems you also need to wait till the element appears.

This seems working:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto("https://www.google.com/maps/place/Donna's+Garden+Flower+Shop+-+Chicago,+IL/@41.9898102,-87.7360212,17z/data=!3m1!4b1!4m5!3m4!1s0x880fce639267ed2f:0x3e47d8ddf3040316!8m2!3d41.9898313!4d-87.7338812?authuser=0&hl=en");
  //await page.screenshot({ path: 'example.png' });

  await page.waitForSelector(".x3AX1-LfntMc-header-title-title span");
  const shopName = await page.$eval(".x3AX1-LfntMc-header-title-title span", span => span.textContent);
  console.log(shopName);

  //await browser.close();
})();