I am trying to get some data from this wikipedia page: https://en.wikipedia.org/wiki/List_of_mango_cultivars
I can get everything that I need except the img src with this code
JavaScript
x
23
23
1
const recordList = await page.$$eval(
2
'div#mw-content-text > div.mw-parser-output > table > tbody > tr',
3
(trows) => {
4
let rowList = []
5
trows.forEach((row) => {
6
let record = { name: '', image: '', origin: '', notes: '' }
7
8
record.image = row.querySelector('a > img').src
9
10
const tdList = Array.from(row.querySelectorAll('td'), (column) => column.innerText)
11
const imageSrc = row.querySelectorAll('a > img').getAttribute('src')
12
13
14
record.name = tdList[0]
15
record.origin = tdList[2]
16
record.notes = tdList[3]
17
rowList.push(record)
18
})
19
20
return rowList
21
}
22
)
23
The error I am getting: Evaluation failed: TypeError: Cannot read properties of null (reading 'src')
Advertisement
Answer
You can wrap your record.image line in a conditional like this
JavaScript
1
4
1
if(row.querySelector('a > img')){
2
record.image = row.querySelector('a > img').src
3
}
4
This will ask if an img inside of an a tag exists, and if it does, then add it to the object.