I am new to javascript and trying to run a javascript function and return a response for this website below. I am trying to use 'document.querySelectorAll' to return a response for all the job titles and the URL for each job posting. How do I return a response for these elements?
Website: https://mckesson.wd3.myworkdayjobs.com/External_Careers
Here is my latest attempt:
function ExecuteScript() {
let nameList = [];
let response = '';
document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
nameList.push(element.innerHTML);
document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
response = nameList[i] '\\t' element.getAttribute('hef') '\\n';
});
return response;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
There are several problems with the example code:
Syntax error: Two
querySelectorAll
are not nested correctly.Misspellings:
hef
should behref
, and it's not enough to store only relative addresses; you need to add domain names in front of them.Because both capture elements of the same class, the two loops can be merged.
Can refer to the following code:
function ExecuteScript() {
let response = '';
document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
response = element.innerHTML '\\t' location.host element.getAttribute('href') '\\n';
});
return response;
}