Last active
February 5, 2024 08:10
-
-
Save kaineer/04233ef48aa5341375f7b5fa8bda4cbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = async (page, scenario, viewport) => { | |
// Элементы настройки | |
// | |
const containerSelector = ".site-navigation"; | |
const contents = ["Программа обучения", "Выпускники", "Как поступить", "Контакты"]; | |
const additionalCount = 2; | |
console.log('SCENARIO > ' + scenario.label); | |
// await require('./clickAndHoverHelper')(page, scenario); | |
// add more ready handlers here... | |
await page.waitForFunction(() => { | |
return document.fonts.ready.then(() => { | |
console.log('Fonts loaded'); | |
return true; | |
}); | |
}); | |
// Все интересное происходит уже здесь | |
// | |
await page.evaluateHandle((params) => { | |
// Получаем параметры, переданные из вызова | |
// (просто использовать переменные из области видимости | |
// не получится) | |
const { contents, containerSelector, additionalCount } = params; | |
// Точка, из которой будем искать | |
const container = document.querySelector(containerSelector); | |
// Отбираем элементы, которые содержат | |
// самый первый текст | |
const children = Array.from(container.querySelectorAll("*")) | |
.filter((e) => e.textContent.trim() === contents[0]); | |
// Ищем элемент, у которого (вместе с ним) | |
// нужное количество элементов одного уровня | |
const e0 = children.find((e) => e.parentElement.childElementCount === contents.length); | |
if (e0) { | |
// Получаем родителя | |
const p0 = e0.parentElement; | |
// Копируем клон первого элемента | |
// нужное количество раз | |
for (let i = 0; i < additionalCount; i++) { | |
p0.appendChild(e0.cloneNode(true)); | |
} | |
} | |
}, {contents, containerSelector, additionalCount}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment