更新:
你好!
在我对上一个问题得到非常有用的答案后,我得到了下一个结果。
为了给你一个简短的解释,我从一个表中过滤了所有 GENTXXXX,使用 GENTXXXX 打开一个包含有关 GENTXXXX 信息的网页,在那里我想强制按下一个按钮来重置所有 GENTXXXX 信息,之后我强制按下一个按钮来确认重置。请检查我的代码,如果您能看看我下面的两个问题,我将非常高兴!非常感谢!
const tds = document.querySelectorAll("#UnitItems td");
let ids = [];
tds.forEach((td) => {
if (td.textContent.includes("GENT")) {
spatieWeg = td.textContent.replace(/\s/g, ""); //removes space in front of "GENT"
ids.push(spatieWeg);
}
});
console.log("extractGent done");
console.log(ids);
debugger; //to check if ids got all the correct values in it.
function processWindow(i) {
var newWindow = window.open("/NetViewer/LineupsCommand/AddUnitInformation?unitID=" + ids[i] + "&lineupName=Z_GENT_1_INT_MAANDAG_1&agencyID=Z_GENT", "_self");
newWindow.addEventListener('load', function() {
var resetButton = newWindow.document.getElementById('btnResetAllEmployees');
if (resetButton) {
resetButton.click();
}
var addButton = newWindow.document.getElementById('btnAddUnitInformation');
if (addButton) {
addButton.click();
}
});
}
for (var i = 0; i < ids.length; i++) {
console.log(ids[i]);
processWindow(i);
}
我现在遇到的问题是这样的。首先,我在“ids”中有 25 个值,当运行函数
processWindow(i)
时,它会从“ids”中获取第 24 个值。不是值 0。之后,由于第二个问题,代码停止运行。
第二个问题,即使打开24the值,它也不会自动执行两次点击。
我已经在按钮前面尝试了
setTimeOut '5000'
。点击。但它只是不“点击”。我使用 setTimeOut 因为我认为页面需要时间才能完全加载。
在控制台中我尝试使用
document.getElementById('btnResetAllEmployees').click()
,这似乎有效。
代码执行的顺序是否正确?
这是代码背后的html,它附加了一个点击事件。
<input type="button" class="secondary-btn" style="position:static;" id="btnResetAllEmployees" value="Wis alle personeel / toestellen">
我不明白,...如果您需要更多信息,我想听听。
非常感谢!
我不会问您为什么要单击新页面上的按钮,但是,我非常乐意回答您的问题。
.forEach
不会倒退。您的选择器的行为更有可能不符合您的预期。"_self"
作为目标。这会导致新窗口替换当前选项卡,从而停止执行代码。为了让您的按钮点击起作用,您需要确保新窗口在新选项卡中打开。我继续清理了你的代码。这消除了很多重复,并且应该修复您遇到的“错误”。
function processWindow(unitId) {
const newWindow = window.open(`/NetViewer/LineupsCommand/AddUnitInformation?unitID=${unitId}&lineupName=Z_GENT_1_INT_MAANDAG_1&agencyID=Z_GENT`);
newWindow.addEventListener('load', function() {
newWindow.document.getElementById('btnResetAllEmployees')?.click();
newWindow.document.getElementById('btnAddUnitInformation')?.click();
});
}
const tds = document.querySelectorAll("#UnitItems td");
tds.forEach((td) => {
if (!td.textContent.includes("GENT")) {
return;
}
const unitId = td.textContent.replace(/\s/g, ""); //removes space in front of "GENT"
processWindow(unitId)
});