I wish to iterate over a table (a calendar table) in Puppeteer and click specific cells (dates) to toggle their status (to "AWAY").
I've included a snippet of the table below. Each td cell contains two child divs, one with the day number (<div class="day_num">
) and another if it has been marked as "AWAY" (<div class="day_content">
).
So far I've been able to scrape the table but that won't allow me to click the actual cells, as scraping just scrapes the table contents into an array.
How can I iterate over all the cells and click specific ones depending on the day number included in the child "day_num"
div? For example, I wish to click the td for day 8 in the example below, to toggle it's status.
<table class="calendar">
<tr class="days">
<td class="day">
<div class="day_num">7</div>
<div class="day_content"></div>
</td>
<td class="day">
<div class="day_num">8</div>
<div class="day_content"></div>
</td>
<td class="day">
<div class="day_num">9</div>
<div class="day_content">AWAY</div>
</td>
The scraping code I currently have is:
const result = await page.evaluate(() => {
const rows = document.querySelectorAll('.calendar tr td div');
return Array.from(rows, (row) => {
const columns = row.querySelectorAll('div');
return Array.from(columns, (column) => column.innerHTML);
});
});
console.log(result);
result is:
[
[], [ '1', '' ], [ '2', 'AWAY' ],
[ '3', '' ], [ '4', '' ], [ '5', '' ],
[ '6', '' ], [ '7', '' ], [ '8', '' ],
[ '9', 'AWAY' ], [ '10', '' ], [ '11', '' ],
[ '12', '' ], [ '13', '' ], [ '14', '' ],
[ '15', '' ], [ '16', '' ], [ '17', '' ],
[ '18', '' ], [ '19', '' ], [ '20', '' ],
[ '21', '' ], [ '22', '' ], [ '23', '' ],
[ '24', '' ], [ '25', '' ], [ '26', '' ],
[ '27', '' ], [ '28', '' ], [ '29', '' ],
[ '30', '' ], [], [],
[], []
]