在 Puppeteer 中存取 javascript 視窗變數
P粉787806024
P粉787806024 2023-09-04 23:32:41
0
1
524
<p>在我的 Puppeteer 腳本中,我使用檔案 JS 在載入頁面之前執行:</p> <pre class="brush:js;toolbar:false;">const preloadFile = fs.readFileSync('./file.js', 'utf8'); const id = await page.evaluateOnNewDocument(preloadFile); </pre> <p>file.js 包含:</p> <pre class="brush:js;toolbar:false;">const i = setInterval(()=>{ if (window.turnstile) { clearInterval(i) window.turnstile.render = (a,b) => { b.userAgent = navigator.userAgent window.tsCallback = b.callback window.action = b.action window.data = b.cData console.log(b) return 'foo' } } },50) </pre> <p>此腳本在瀏覽器控制台上列印一個物件(操作、Cdata、回呼...)</p> <p>我想從pupetter端取得這個物件並且能夠呼叫函數callback()。 </p> <p>我使用了 page.evaluate(),但對我不起作用</p>
P粉787806024
P粉787806024

全部回覆(1)
P粉193307465

您可以使用我的Puppeteer 包裝庫https://github.com/wix-incubator/ bubanai/pulls 來實現這個目標(從瀏覽器控制台取得物件),使用 new ConsoleDriver(context).getMessages() 方法,然後將接收到的物件從陣列轉換為JSON...

或只是將 ConsoleDriver 程式碼複製到您的專案中。

參考

使用範例

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!