Estoy tratando de establecer un valor para un elemento Input/TextArea en html usando titiritero sin éxito. El código se transpila con mecanografiado.
Aquí uso YouTube como ejemplo.
Primero debe navegar a youtube, luego seleccionar el cuadro de entrada con el selector css de pseudoclase y establecer el valor de acuerdo con las variables: prefijo y título.
El script se cierra tan pronto como comienza a ejecutar page.eval. No estoy seguro si estoy usando el selector css incorrecto. Puede haber un problema cuando intento asignar una cadena a la propiedad "valor" también.
(async ()=>{ const searchbox='html>body>ytd-app>div>div>ytd-masthead>div:nth-of-type(3)/div:nth-of-type(2)/ytd-searchbox/form/div:first-of-type/div:first-of-type/input' const puppeteer = require("puppeteer") const browser = await puppeteer.launch({headless:false, defaultViewport: null, slowMo: 100}); const page = await browser.newPage(); await page.goto('https://www.youtube.com/'); await uploadPhoto("Some", " Video"); async function uploadPhoto(prefix:string, caption:string) { await page.evaluate( (searchbox:string, prefix:string, caption:string) => { (document.querySelector(searchbox) as HTMLInputElement).value = `${prefix} ${caption}`}, searchbox, prefix, caption) } console.log("finish") })();Creo que debería eliminar todos los tipos de su función de evaluate , ya que esta función se ejecutará directamente en el navegador y creo que los tipos harían que fallara:
async function uploadPhoto(prefix:string, caption:string) { await page.evaluate( (searchbox, prefix, caption) => { document.querySelector(searchbox).value = `${prefix} ${caption}` }, searchbox, prefix, caption) } Una solución más fácil es usar el type de método de la clase ElementHandle :
const searchInput: ElementHandle<Element> = await page.waitForSelector(searchbox, { timeout: 5000 }); await searchInput.type("Some text to search"); o type desde la clase de Page :
await page.type(searchbox, "Some text to search");