到目前为止,我已经尝试了两件事。
const iconupload = await page.$x(xpath); //Triple-checked that this x path is correct
iconupload[0].uploadFile(picture); //Triple-checked that this picture path is correct
和
await page.evaluate((picturepath) => {
document.querySelector('input[type=file]').value = picturepath
}, picturepath);
对于第一种方法,我得到一个
TypeError: Cannot read property 'uploadFile' of undefined
对于第二种方法,我得到了
Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
有什么方法可以将图像上传到此输入吗?
解决了
原来,所讨论的图像输入在iframe元素内。进行一些挖掘之后,我发现如果页面内包含iframe元素,则正常的伪造者功能将无法正常工作。这是因为根据文档,典型的伪造者功能(例如page。$ x,page。$和page.waitFor)实际上是page.mainFrame()。$ x,page.mainFrame()。$和page的快捷方式。 mainFrame()。waitFor()。因此,这些方法不适用于页面中包含的辅助框架
这是我为克服此问题所做的工作。我首先从src属性获得了iframe的链接。然后,我创建了一个新页面,并让该新页面转到了此链接。然后,我可以照常刮擦。这是我的代码的一段摘要:
let framelink = await page.evaluate(() => {
return document.getElementsByTagName('iframe')[1].src;
});
const picturepage = await browser.newPage();
picturepage.goto(framelink)
//Now I can just scrape normally using methods like picturepage.$x
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句