Jakki
Jakki

Reputation: 37

Error while scraping webpage in puppeteer js

I am scraping a webpage using puppeteer js. I am writing the following line to fetch the seller's name. On some pages, it has a seller name and on some pages, it does not have any seller name.

let seller = document.querySelector(".seller-name").textContent;

When a page has a seller name it gives me that text. But when it does not have any seller name it gives me the following error.

Fatal Error: Evaluation failed: TypeError: Cannot read property 'textContent' of null

Can anyone please help me to solve this problem?

Upvotes: 0

Views: 82

Answers (2)

Waseem Ansar
Waseem Ansar

Reputation: 123

Just check if element exist before getting text content.

const sellerDiv = document.querySelector(".seller-name")
let seller = ""

if(sellerDiv) {
    seller = sellerDiv.textContent;
} else {
    seller = ""
}

console.log(seller)

Upvotes: 1

KLP
KLP

Reputation: 433

You'll want to guard against there being no elements that match that selector on the page. You should be able to simply do:

let sellerEl = document.querySelector(".seller-name");
if (sellerEl) {
  let seller = sellerEl.textContent;
}

The "truth value" of null is false, so the code inside the 'if' block will not execute if there is no element with the class seller-name on the page.

Upvotes: 1

Related Questions