3

I want to select and click the specified <a> of next sibling, html code below:

<ul>
  <li>
    <a></a>
  </li>
  <li>
    <a></a>
  </li>
  <li class='abc'>
    <a></a>
  </li>
  <li>
    <a></a> <!-- I want to click this link -->
  </li>
</ul> 

the <ul> is changing, but the <li class='abc'> is changeless

and use the code below:

    let tag = await page.evaluate(() => {
        return document.querySelector('li.abc').nextSibling.a
    })

    console.log(tag?'Y': 'N')
    // await page.click(tag)

then it output 'N'

Can someone help me fix this error? Thanks in advance!

marslord
  • 103
  • 1
  • 8

1 Answers1

4

Got this answer from Dave Batiste(user of puppeteer.slack.com),

Thanks to @Vaviloff point that: I should post this answer.

Use the code page.click('.abc + li > a') can click the specified <a> that I want to, and Dave Batiste(user of puppeteer.slack.com) give more info as below:

If you do want a handle that you can call methods on, I think you'd be interested in > page.$...

const handle = await page.$(selector);
handle.click(options);
marslord
  • 103
  • 1
  • 8