gpt4 book ai didi

javascript - 如何等待 javascript 中出现元素?

转载 作者:行者123 更新时间:2023-12-02 22:08:25 27 4
gpt4 key购买 nike

(function() {
document.evaluate('/html/body/nav/section[4]/div/form[1]/input[4]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();
Sleep(15);
window.close();
})();

所以点击功能可以工作,但是一旦我添加 sleep windows.close 就不再工作了?我还在学习 Javascript,有人可以帮忙吗?我想做的就是让它单击“投票”按钮,然后关闭事件选项卡。

最佳答案

尝试这个函数,它将在元素存在时等待。

function waitWhileElementPresent(cssLocator, timeoutInSeconds) {
var currentTime = new Date().getTime();
var endTime = currentTime + timeoutInSeconds * 1000;
var checkExist = setInterval(function () {
if (document.querySelectorAll(cssLocator).length == 0) {
clearInterval(checkExist);
console.log('waited until element not present.');
return;
} else if (endTime < new Date().getTime()) {
clearInterval(checkExist);
console.log('element still found after ' + timeoutInSeconds + ' seconds.');
return;
} else {
console.log('waiting for element not present ...');
}
}, 100);
}

这是另一个函数,它将等待元素出现

function waitUntilElementPresent(cssLocator, timeoutInSeconds) {
var currentTime = new Date().getTime();
var endTime = currentTime + timeoutInSeconds * 1000;
var checkExist = setInterval(function () {
if (document.querySelectorAll(cssLocator).length) {
clearInterval(checkExist);
return;
} else if (endTime < new Date().getTime()) {
clearInterval(checkExist);
console.log('not found in specified time.');
return;
} else {
console.log('waiting for element to be present…');
}
}, 100);
}

关于javascript - 如何等待 javascript 中出现元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com