chromancer
Version:
A powerful command-line interface for automating Chrome browser using Playwright. Perfect for web scraping, automation, testing, and browser workflows.
77 lines (76 loc) • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitForElement = waitForElement;
exports.isElementVisible = isElementVisible;
exports.getElementInfo = getElementInfo;
/**
* Wait for an element to appear on the page
*/
async function waitForElement(page, selector, options = {}) {
const { timeout = 30000, state = 'attached' } = options;
try {
// In Playwright, we use waitForSelector which returns ElementHandle or null
const element = await page.waitForSelector(selector, {
timeout,
state,
});
if (!element) {
throw new Error(`Element not found: ${selector}`);
}
return element;
}
catch (error) {
if (error.message?.includes('Timeout') || error.message?.includes('Waiting failed')) {
throw new Error(`Element not found: ${selector}`);
}
throw error;
}
}
/**
* Check if an element is visible on the page
*/
async function isElementVisible(page, selector) {
try {
const element = await page.$(selector);
if (!element)
return false;
return await element.isVisible();
}
catch {
return false;
}
}
/**
* Get comprehensive information about an element
*/
async function getElementInfo(page, selector) {
const element = await page.$(selector);
if (!element)
return null;
return await element.evaluate((el) => {
const style = window.getComputedStyle(el);
const rect = el.getBoundingClientRect();
const info = {
text: el.textContent?.trim() || '',
value: el.value || '',
tagName: el.tagName,
id: el.id,
className: el.className,
isVisible: (style.display !== 'none' &&
style.visibility !== 'hidden' &&
style.opacity !== '0' &&
rect.width > 0 &&
rect.height > 0),
isDisabled: el.disabled || false,
};
// Add element-specific properties
if (el.tagName === 'A') {
info.href = el.href;
}
if (el.tagName === 'INPUT') {
info.type = el.type;
info.placeholder = el.placeholder;
}
return info;
});
}