[英]executing javascript code thought chrome dev tools protocol
我正在嘗試通過 CDTP 和 PyChromeDevTools 庫執行一些 javascript 代碼。 以下代碼有效:
從終端:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
import PyChromeDevTools
chrome.Network.enable()
chrome.Page.enable()
script="t='hi,';c='this code works on the same line';console.log(t,c)"
chrome.Runtime.evaluate(expression=script)
當我需要注入不在同一行的復雜 js 代碼時,我遇到了一些麻煩,例如 function 和 javascript 請求 ecc ..
script="t='hi,';
c='this code works not on the same line don't work ';
console.log(t,c)"
chrome.Runtime.evaluate(expression=script)
運行時評估是否可能等於 javascript 文件? 像這樣:
chrome.Runtime.evaluate(expression=file.js)
CDTP 文件
您應該只將文件中的 js 代碼加載到 str 變量中
def load_text(filename, mode='r', encoding='utf-8'):
with open(filename, mode, encoding=encoding) as f:
txt = f.read()
return txt
file_js = load_text('file.js')
chrome.Runtime.evaluate(expression=file_js)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.