Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer get all <a> href links

Hello I am trying to scrape a web page and return all of the links inside example of the html element:

<a href="#/item/2sDSXbG">
<a href="#/item/4ssaSXbG">
<a href="#/item/Sawd432">

Here is my code:

let links = [];
let elements2 = document.querySelectorAll('a');
  for (var element2 of elements2)
  links.push(element2.textContent);

After I return the value and print it I get an Error telling me that my variable is not defined My Error:

UnhandledPromiseRejectionWarning: ReferenceError: links is not defined

End Goal: My goal is to be able to be able to create an array of all the items in the list. I would than later parse the information so that it is just the text after /item/

like image 911
Frank Ford Avatar asked Jan 29 '26 16:01

Frank Ford


2 Answers

With $$eval:

let hrefs = await page.$$eval('a', as => as.map(a => a.href))
like image 120
pguardiario Avatar answered Feb 01 '26 07:02

pguardiario


It seems this is what you need to achieve your goal with puppeteer:

const hrefs = await page.evaluate(() => {
  let links = [];
  let elements2 = document.querySelectorAll('a');
  for (let element2 of elements2)
    links.push(element2.href);
  return links;
});
like image 43
vsemozhebuty Avatar answered Feb 01 '26 05:02

vsemozhebuty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!