[英]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.