admin管理员组文章数量:1431695
I want to use the remote socket debugging stuff for Chrome devtools (link) from Python. I'm using code adapted from here.
I've managed to get ping
and list_tabs
working. But I can't figure out how to evaluate_javascript
. Can anybody tell me what I'm doing wrong?
import subprocess
import time, json, socket
from jca.files import my_paths
def request(tool, destination=None, **kw):
# Send a mand via socket to 'DevToolsService' or 'V8Debugger'
j = json.dumps(kw)
request = 'Content-Length:%d\r\nTool:%s\r\n' % (len(j), tool)
if destination:
request += 'Destination:%s\r\n' % (destination,)
request += '\r\n%s\r\n' % (j,)
sock.send(request)
if kw.get('mand', '') not in RESPONSELESS_COMMANDS:
time.sleep(.1)
response = sock.recv(30000)
if response.strip():
j = response.split('\r\n\r\n', 1)[1]
return json.loads(j)
if __name__ == '__main__':
proc = subprocess.Popen('"%s" --remote-shell-port=9222' % my_paths.chrome_exe)
RESPONSELESS_COMMANDS = ['evaluate_javascript']
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 9222))
sock.send('ChromeDevToolsHandshake\r\n')
result = sock.recv(1024)
print 'ping: ', request('DevToolsService', mand='ping')
time.sleep(4)
print 'list_tabs: ', request('DevToolsService', mand='list_tabs')
request('V8Debugger', mand='evaluate_javascript',
data='javascript:window.location.reload()')
sock.close()
print 'done'
I want to use the remote socket debugging stuff for Chrome devtools (link) from Python. I'm using code adapted from here.
I've managed to get ping
and list_tabs
working. But I can't figure out how to evaluate_javascript
. Can anybody tell me what I'm doing wrong?
import subprocess
import time, json, socket
from jca.files import my_paths
def request(tool, destination=None, **kw):
# Send a mand via socket to 'DevToolsService' or 'V8Debugger'
j = json.dumps(kw)
request = 'Content-Length:%d\r\nTool:%s\r\n' % (len(j), tool)
if destination:
request += 'Destination:%s\r\n' % (destination,)
request += '\r\n%s\r\n' % (j,)
sock.send(request)
if kw.get('mand', '') not in RESPONSELESS_COMMANDS:
time.sleep(.1)
response = sock.recv(30000)
if response.strip():
j = response.split('\r\n\r\n', 1)[1]
return json.loads(j)
if __name__ == '__main__':
proc = subprocess.Popen('"%s" --remote-shell-port=9222' % my_paths.chrome_exe)
RESPONSELESS_COMMANDS = ['evaluate_javascript']
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 9222))
sock.send('ChromeDevToolsHandshake\r\n')
result = sock.recv(1024)
print 'ping: ', request('DevToolsService', mand='ping')
time.sleep(4)
print 'list_tabs: ', request('DevToolsService', mand='list_tabs')
request('V8Debugger', mand='evaluate_javascript',
data='javascript:window.location.reload()')
sock.close()
print 'done'
Share
Improve this question
asked Jul 11, 2011 at 0:47
Jesse AldridgeJesse Aldridge
8,16910 gold badges50 silver badges78 bronze badges
2 Answers
Reset to default 2I'm sorry for spam, there's a Java library for this: http://code.google./p/chromedevtools/
Since you probably chosen Python not at random, you could have it as a reference implementation if running Java code is OK for you. I guess you could check the actual messages sent and received from Java debugger.
The problem was that I didn't set a tab_id for destination. Adding destination=2 to the request call fixes the problem.
本文标签: javascriptUsing remote chrome devtools from PythonStack Overflow
版权声明:本文标题:javascript - Using remote chrome devtools from Python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745586010a2664911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论