지금까지 두 가지를 시도했습니다.
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 (). $ 및 페이지에 대해 SHORTCUTS이기 때문입니다. 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] 삭제
몇 마디 만하겠습니다