处理分页,点击元素 parent

handling pagination , clicking on element parent

我正在尝试处理我页面中的分页,分页 link 是这样的

<a href="nextpage.php"> <img src="pic.jpg" id="next"></a>

这是我点击这个的代码 link

 while (has_next_page)
{
      // do some data scraping 

            if ( await page.$("img#next"))
            {

                console.log('clicking on next page');
                await  page.click('#next');

                await Promise.all([
                    page.waitForNavigation() ,
                    page.waitForSelector('#rowTbl'),
                ]).catch(function(){
                    throw new Error(tab_id + ' error waitning for rowTbl');
                })

            }
            else
                has_next_page = false ;
}

出于某种原因,这不起作用,即使单击它也不会导航到下一页..所以我想也许我必须单击 a 选项卡而不是 img ...所以这是我的问题,我怎样才能点击 img#next parent ?

您应该可以使用 .evaluate() 来触发无法使用选择器的元素上的点击事件。

await page.evaluate(() => { document.querySelector("#next").parentNode.click(); });