Добавить get_mui_table_playwright.py
This commit is contained in:
95
get_mui_table_playwright.py
Normal file
95
get_mui_table_playwright.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
|
SITE = "https://10.0.5.145/connection"
|
||||||
|
|
||||||
|
LOGIN = "admin"
|
||||||
|
PASSWORD = "admin"
|
||||||
|
|
||||||
|
js_script = """
|
||||||
|
(() => {
|
||||||
|
const scrollContainer = document.querySelector('.MuiDataGrid-virtualScroller');
|
||||||
|
if (!scrollContainer) return { error: 'Не нашёл контейнер скролла таблицы' };
|
||||||
|
|
||||||
|
const getVisibleRows = () => [...document.querySelectorAll('[data-rowindex]')];
|
||||||
|
|
||||||
|
const extractRow = (rowEl, fields) => {
|
||||||
|
const cells = [...rowEl.querySelectorAll('[role="gridcell"]')];
|
||||||
|
const rowObj = {};
|
||||||
|
cells.forEach((cell, i) => {
|
||||||
|
const field = fields[i] || 'col_' + i;
|
||||||
|
let value = cell.innerText.trim();
|
||||||
|
const checkbox = cell.querySelector('input[type="checkbox"]');
|
||||||
|
if (checkbox) {
|
||||||
|
value = checkbox.value || checkbox.checked.toString();
|
||||||
|
} else if (!value) {
|
||||||
|
value = cell.getAttribute('title') || (cell.querySelector('[title]')?.getAttribute('title')) || '';
|
||||||
|
}
|
||||||
|
rowObj[field] = value;
|
||||||
|
});
|
||||||
|
return rowObj;
|
||||||
|
};
|
||||||
|
|
||||||
|
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
const seen = new Set();
|
||||||
|
const rows = [];
|
||||||
|
const maxStuck = 8;
|
||||||
|
let stuckCount = 0;
|
||||||
|
|
||||||
|
const fields = [...document.querySelectorAll('.MuiDataGrid-columnHeader')]
|
||||||
|
.map(col => col.dataset.field);
|
||||||
|
|
||||||
|
return (async () => {
|
||||||
|
while (stuckCount < maxStuck) {
|
||||||
|
const visible = getVisibleRows();
|
||||||
|
for (const rowEl of visible) {
|
||||||
|
const idx = Number(rowEl.dataset.rowindex);
|
||||||
|
if (!seen.has(idx)) {
|
||||||
|
seen.add(idx);
|
||||||
|
rows[idx] = extractRow(rowEl, fields);
|
||||||
|
stuckCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const before = seen.size;
|
||||||
|
scrollContainer.scrollBy(0, 500);
|
||||||
|
await sleep(200);
|
||||||
|
const after = seen.size;
|
||||||
|
if (after === before) stuckCount++;
|
||||||
|
}
|
||||||
|
return rows;
|
||||||
|
})();
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
|
||||||
|
with sync_playwright() as p:
|
||||||
|
browser = p.chromium.launch(headless=False)
|
||||||
|
context = browser.new_context(ignore_https_errors=True)
|
||||||
|
page = context.new_page()
|
||||||
|
|
||||||
|
page.goto(SITE)
|
||||||
|
page.wait_for_selector(".login-form", timeout=15000)
|
||||||
|
|
||||||
|
# авторизация
|
||||||
|
page.fill('input[name="Login"]', LOGIN)
|
||||||
|
page.fill('input[name="Password"]', PASSWORD)
|
||||||
|
page.click('.login-form button[type="submit"]')
|
||||||
|
|
||||||
|
# ждём появления таблицы после авторизации
|
||||||
|
page.wait_for_selector(".MuiDataGrid-virtualScroller", timeout=30000)
|
||||||
|
|
||||||
|
# сбор таблицы
|
||||||
|
start_time = time.time()
|
||||||
|
result = page.evaluate(js_script)
|
||||||
|
total_time = time.time() - start_time
|
||||||
|
|
||||||
|
if "error" in result:
|
||||||
|
print("Ошибка:", result["error"])
|
||||||
|
else:
|
||||||
|
print(json.dumps(result, indent=2, ensure_ascii=False))
|
||||||
|
print(f"Всего строк: {len(result)}")
|
||||||
|
print(f"Время выполнения: {total_time:.2f} секунд")
|
||||||
|
|
||||||
|
browser.close()
|
||||||
Reference in New Issue
Block a user