Не работайте, нажмите на расширение JavaScript в iframevJavascript

Форум по Javascript
Ответить
Anonymous
 Не работайте, нажмите на расширение JavaScript в iframev

Сообщение Anonymous »

Если переменная соответствует элементу в видимой части, код работает нормально, происходит щелчок. Если переменная соответствует элементу, которого нет в видимой части, код работает нормально до щелчка, щелчок не происходит с 20 попытками. Как сделать так, чтобы после прокрутки до желаемого элемента происходит щелчок < /p>
function click(cleanTableId) {
console.log("✅ start click");

const allElements = document.querySelectorAll("div[data-observer-id]");
const allMatches = [];

allElements.forEach((container) => {
const observerId = container.getAttribute("data-observer-id");
const baseId = observerId.replace(/-P.*/, "");
< /code>
Все элементы в iframe собираются в переменную и начинают сравниваться с CleanTableId < /p>
if (baseId === cleanTableId) {
console.log(`✅ Match found: ${observerId} → Scrolling into view`);
allMatches.push(observerId);

const img = container.querySelector("img");
const target = img || container;
< /code>
Элемент «IMG» добавляется в соответствующий элемент, а метод прокрутки применяется и заполняет нажмите на элемент < /p>
container.scrollIntoView({ behavior: "smooth", block: "center" });
target.click();

const isVisible = target.offsetParent !== null && !target.disabled;
< /code>
Состояние создается для определения элемента в визуальной части, и щелчок происходит с помощью SetInterval < /p>
if (isVisible) {
console.log("✅ Element is visible, attempting click...");

setTimeout(() => {
let attempts = 0;
const maxAttempts = 20;

const tryClick = setInterval(() => {
container.scrollIntoView({ behavior: "smooth", block: "center" });
target.click();
attempts++;
console.log("✅ click", attempts);
if (attempts >= maxAttempts) {
console.warn("❌ Click attempts timed out after 20 tries");
clearInterval(tryClick);
}
}, 100);
}, 700);
}
}
});
console.log("✅ Clicked elements matching:", allMatches);
}
< /code>
Это консоль, когда элемент не находится в визуальной части < /p>
✅ start click
iframe-script.js:36 ✅ Match found: 412-P0-38 → Scrolling into view
iframe-script.js:48 ✅ Element is visible, attempting click...
iframe-script.js:68 ✅ Clicked elements matching: ['412-P0-38']
iframe-script.js:58 ✅ click 1
iframe-script.js:58 ✅ click 2
iframe-script.js:58 ✅ click 3
iframe-script.js:58 ✅ click 4
iframe-script.js:58 ✅ click 5
iframe-script.js:58 ✅ click 6
iframe-script.js:58 ✅ click 7
iframe-script.js:58 ✅ click 8
iframe-script.js:58 ✅ click 9
iframe-script.js:58 ✅ click 10
iframe-script.js:58 ✅ click 11
iframe-script.js:58 ✅ click 12
iframe-script.js:58 ✅ click 13
iframe-script.js:58 ✅ click 14
iframe-script.js:58 ✅ click 15
iframe-script.js:58 ✅ click 16
iframe-script.js:58 ✅ click 17
iframe-script.js:58 ✅ click 18
iframe-script.js:58 ✅ click 19
iframe-script.js:58 ✅ click 20
iframe-script.js:60 ❌ Click attempts timed out after 20 tries
< /code>
Это консоль, когда элемент находится в визуальной части < /p>
✅ start click
iframe-script.js:36 ✅ Match found: 412-P0-38 → Scrolling into view
iframe-script.js:48 ✅ Element is visible, attempting click...
iframe-script.js:68 ✅ Clicked elements matching: ['412-P0-38']


Подробнее здесь: https://stackoverflow.com/questions/795 ... in-iframev
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»