Puppeteer只关注按钮而不是单击按钮

问题描述 投票:0回答:2

尝试自动点击网站page上的按钮的过程,但它只关注按钮而不是点击它。

我尝试过使用puppeteer click()函数和焦点+按下输入功能,它们都没有工作

const puppeteer = require('puppeteer');

async function run() {

const browser = await puppeteer.launch({headless:true});

  const page = await browser.newPage();
const BUTTON_SELECTOR = 'body > section > section > header > div.reply-button-row > button';      
  await page.goto('https://bozeman.craigslist.org/zip/d/bozeman-panasonic-36-tv/6837588995.html')
await page.waitFor(2000); 

await page.waitFor(BUTTON_SELECTOR);

await page.click(BUTTON_SELECTOR)
//await page.focus(BUTTON_SELECTOR)
//await page.keyboard.press('Enter');
await page.screenshot({ path: 'screenshots/image.png' });  
  browser.close();
}

run();

代码输出图片:https://imgur.com/m0CYqNiqwe

预期输出图像:https://imgur.com/Hmg3BgVasd

node.js web-scraping puppeteer google-chrome-headless headless-browser
2个回答
2
投票

它点击,但屏幕截图过早,直到显示新块。您可以等待一段时间或等待创建块并显示:

await page.click(BUTTON_SELECTOR)
await page.waitFor(2000);
await page.screenshot({ path: 'image.png' });

要么

await page.click(BUTTON_SELECTOR)
await page.waitForSelector('div.reply-info aside.reply-flap', { visible: true });
await page.screenshot({ path: 'image.png' });

0
投票

请尝试使用此选择器:

const BUTTON_SELECTOR = 'button[data-href^="/__SERVICE_ID"]'
© www.soinside.com 2019 - 2024. All rights reserved.