简体   繁体   中英

Puppeter - Link inside an iFrame

I have to get the advertising link below the bullet points of this page .

I am trying with Puppeter but I am having trouble because the Ad is an iframe!

I can successfully get what I need using Chrome console:

document.querySelector('#adContainer a').href

Puppetter

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  page.setViewport({width: 1440, height: 1000})
  await page.goto('https://www.amazon.co.uk/dp/B07DDDB34D', {waitUntil: 'networkidle2'})

  await page.waitFor(2500);

  const elementHandle = await page.$eval('#adContainer a', el => el.href);

  console.log(elementHandle);
  await page.screenshot({path: 'example.png', fullPage: false});

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

Error: Error: failed to find element matching selector "#adContainer a"

在此处输入图片说明

EDIT:

const browser = await puppeteer.launch();
  const page = await browser.newPage();
  page.setViewport({width: 1440, height: 1000})
  await page.goto('https://www.amazon.co.uk/dp/B07DDDB34D', {waitUntil: 'networkidle2'})

const adFrame = page.frames().find(frame => frame.name().includes('"adServer":"cs'))
const urlSelector = '#sp_hqp_shared_inner > div > a';
const url = await adFrame.$eval(urlSelector, element => element.textContent);
console.log(url);


  await browser.close();

Run : https://try-puppeteer.appspot.com/

You need to do that query inside the frame itself, which can be accessed via page.frames() :

const adFrame = page.frames().find(frame => frame.name().includes('<some text only appearing in name of this iFrame>');
const urlSelector = '#sp_hqp_shared_inner > div > a';
const url = await adFrame.$eval(urlSelector, element => element.textContent);
console.log(url);

How I got the selector of that url: 在此处输入图片说明

Discaimer I haven't tried this myself. Also, I think the appropriate way to get that url inside the iFrame is something more like this :

const url = await adFrame.evaluate((sel) => {
  return document.querySelectorAll(sel)[0].href;
}, urlSelector);

You have to switch to the frame you want to work on every time the page loads.

async getRequiredLink() {
    return await this.page.evaluate(() => {
        let iframe = document.getElementById('frame_id'); //pass id of your frame
        let doc = iframe.contentDocument; // changing the context to the working frame
        let ele = doc.querySelector('you-selector'); // selecting the required element
        return ele.href;
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM