0

I'm trying to find all "a" tag elements which attribute(href) contains part of the text "/groups/" and after click on it(one of them), and I want to exclude links which includes "/user/" text in attribute

I search for links like I show in the image

Here is my code example:

        const search = await this.page.$$eval('a[href^=\'/groups/\']', links => {
        return links
            .map(e => e.href)
            .filter(e => !e.getAttribute('href').includes('user'))
            .forEach((link, indx) => (indx === 2) && link.click() );
    });

But I've got an errors like:

Cannot read properties of undefined (reading 'getAttribute')

or

Cannot read properties of undefined (reading 'includes')

  • 2
    You don't need to call `getAttribute` because you already returned the href in the map method. – User456 Jul 21 '22 at 07:53
  • But if I delete getAttribute('href') I will take an error like: Cannot read properties of undefined (reading 'includes') – Александр Мерный Jul 21 '22 at 07:55
  • Can you try logging `e` in the map method? – User456 Jul 21 '22 at 07:57
  • There is a problem. Because console.log does not work inside $$eval method – Александр Мерный Jul 21 '22 at 08:02
  • Why are you adding `> span` in your selector? What do you think it does? – User456 Jul 21 '22 at 08:06
  • Ohh, my bad. I forgot to remove them, it's from another example. But it does not resolve my issue. I've got: link.click is not a function – Александр Мерный Jul 21 '22 at 08:13
  • 1
    Yes, you are getting this error because `link` is a string (the href). Remove the map method and change the validation in filter to `!e.href.includes('user')` – User456 Jul 21 '22 at 08:15
  • If you are not able to add a console.log maybe you can still add a breakpoint from the browser? See https://developer.chrome.com/docs/devtools/javascript/breakpoints/ – Giorgio Tempesta Jul 21 '22 at 08:18
  • I would remove the `map` method rather than `getAttribute` becuase there's no way to click on strings. Please show the site you're working with so others can run the code and reproduce the problem. See [mcve]. You can monitor logs in Puppeteer with [this](https://stackoverflow.com/a/60075804/6243352). I suggest updating the code to fix the original `getAttribute` problem and show where you're at now. Thanks. – ggorlen Jul 21 '22 at 17:16

0 Answers0