web-crawling-utils
Version:
Common useful utils for web crawling and automation scripts
57 lines (56 loc) • 3.17 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.typeIntoElement = exports.clickElement = void 0;
/**
* Clicks on a specified element within a Puppeteer page after waiting for it to appear.
*
* @async
* @function clickElement
* @param {Page} page - The Puppeteer page instance on which the element is located.
* @param {string} selector - The CSS selector for the target element.
* @param {number} [waitTime=3000] - Maximum wait time (in milliseconds) to locate the element. Default is 3000ms.
* @returns {Promise<void>} - A Promise that resolves once the click action is performed.
*
* @description This function helps to interact with web elements by waiting for the target element
* to be available within the specified time, then simulating a click action on the element.
* This utility is commonly used in automation workflows where an element's presence is not immediate.
*
* @example
* await clickElement(page, '#submit-button');
*/
const clickElement = (page_1, selector_1, ...args_1) => __awaiter(void 0, [page_1, selector_1, ...args_1], void 0, function* (page, selector, waitTime = 3000) {
yield page.waitForSelector(selector, { timeout: waitTime });
yield page.click(selector);
});
exports.clickElement = clickElement;
/**
* Types text into a specified input element within a Puppeteer page after waiting for it to appear.
*
* @async
* @function typeIntoElement
* @param {Page} page - The Puppeteer page instance on which the element is located.
* @param {string} selector - The CSS selector for the target input element.
* @param {string} text - The text to be typed into the input element.
* @param {number} [waitTime=3000] - Maximum wait time (in milliseconds) to locate the element. Default is 3000ms.
* @returns {Promise<void>} - A Promise that resolves once the text is typed into the element.
*
* @description This function waits for an input element to appear, then types the specified text
* into it. This is useful for automating form filling and text input in web-based applications.
*
* @example
* await typeIntoElement(page, '#username', 'myUser123');
*/
const typeIntoElement = (page_1, selector_1, text_1, ...args_1) => __awaiter(void 0, [page_1, selector_1, text_1, ...args_1], void 0, function* (page, selector, text, waitTime = 3000) {
yield page.waitForSelector(selector, { timeout: waitTime });
yield page.type(selector, text);
});
exports.typeIntoElement = typeIntoElement;