I will use the StackOverflow page you are looking at now as an example.
Access each question link in the sidebar's“Related”by fetch Get the element of the Related page (in the example, the title of the page) with querySelector To make an array of the retrieved elements
The code written is as follows
Please paste it into the console on this page so you can see what it says.
I am unable to store it in an array and would appreciate your guidance.
let relatedPageURL = document.querySelectorAll(".sidebar-related .js-gps-track");
let relatedPageURLarray = [];
let relatedPageHTMLtitleArray = [];
for (let i = 0; i < relatedPageURL.length; i++) {
relatedPageURLarray[i] = relatedPageURL[i].getAttribute("href");
fetch(relatedPageURLarray[i])
.then((response) => response.text())
.then((text) => {
let DOMtext = new DOMParser();
let relatedPageHTML = DOMtext.parseFromString(text, "text/html");
let relatedPageHTMLtitle = relatedPageHTML.querySelector("#question-header");
relatedPageHTMLtitleArray[i] = relatedPageHTMLtitle;
console.log(relatedPageHTMLtitleArray);
});
}
//result []
//Expected Results [div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column, div#question-header.d-flex.sm:fd-column]