Using puppeteer I am trying to achieve something where I can capture network responses on selection of a dropdown item. It does not have to be immediate, we can select all options one after another, and can use network response later as well, as long as it works smoothly.
(async () => {
const browser = await puppeteer.launch(chromeoptions);
const page = await browser.newPage();
page.target().createCDPSession();
page.setDefaultNavigationTimeout(1000000);
}
async function getAppointmentData() {
await page.goto('go to the page URL')
let appointmentData = [];
await page.waitForSelector(
"[id*=dropdownId]"
);
for (let option of options) {
// Selecting an option from a dropdown trigger a network call that I need to record
await page.select(
"select#dropdownId",
option?.id
)
/* Check for a network call that contains that option?.id or .json?details
Sample URL >>>> https://abc.test.com/en/10.json?details [Where 10 is options?id]
*/
// After All Items in dropdown is selected, Want to insert response [Array of Objects] to an object like:
appointmentData.push({
id: options?.id;
data: [Array of Objects]
}]
} // For Loop Ends
processAppointments(appointmentData )
}
processAppointments(data) {
///
}
getAppointmentData();
})();
I tried using page.on() and other methods but not helpful. can someone please help me out here? been stuck for almost one week.
Your page.on should look something like:
page.on("response", response => {
let match = response.url().match(/(\d+).json/)
if(match){
console.log(`option ${match[1]}`)
}
})