尝试自动点击网站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
它点击,但屏幕截图过早,直到显示新块。您可以等待一段时间或等待创建块并显示:
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' });
请尝试使用此选择器:
const BUTTON_SELECTOR = 'button[data-href^="/__SERVICE_ID"]'