Skip to content
Advertisement

Can I simplify this code to avoid the type error for reading properties?

I am writing this code to scrape a webpage. I need to get specific information from the website and there is a lot of information needed to be scraped.

The code that I write works but when do it repeatedly it encounters error on some of the line, e.g. line 20, line 24.

Below is the code

JavaScript

There are like 13 data I need to scrape.

The error that I got is

const salary = await (await el4.getProperty(‘textContent’)).jsonValue(); TypeError: Cannot read properties of undefined (reading ‘getProperty’)

Advertisement

Answer

The quick fix would be to check if the destructured ElementHandle actually exists before trying to call getProperty on it, for example:

JavaScript

A less repetitive script would look more like:

JavaScript

And then iterate through the results object.

Advertisement