-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyChromeDevTools.py
More file actions
156 lines (136 loc) · 4.86 KB
/
PyChromeDevTools.py
File metadata and controls
156 lines (136 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python3
import json
import time
import requests
import websocket
from Exceptions import AutoChromeException
TIMEOUT = 1
class GenericElement(object):
def __init__(self, name, parent):
self.name = name
self.parent = parent
def __getattr__(self, attr):
func_name = '{}.{}'.format(self.name, attr)
def generic_function(**args):
self.parent.pop_messages()
self.parent.message_counter += 1
message_id = self.parent.message_counter
params = {}
for key in args:
if args[key] is not None:
params[key] = args[key]
call_obj = {'id': message_id, 'method': func_name, 'params': params}
data = json.dumps(call_obj)
print('>>> send: ' + data)
self.parent.ws.send(data)
result, _ = self.parent.wait_result(message_id)
if result is not None:
if 'error' in result:
raise AutoChromeException(result['error']['data'])
else:
return result['result']
else:
return None
return generic_function
class ChromeInterface(object):
message_counter = 0
def __init__(self, host='localhost', port=9222, tab=0, timeout=TIMEOUT, auto_connect=True):
self.host = host
self.port = port
self.ws = None
self.tabs = None
self.timeout = timeout
if auto_connect:
self.connect(tab=tab)
def get_tabs(self):
response = requests.get('http://{}:{}/json'.format(self.host, self.port))
self.tabs = json.loads(response.text)
def connect(self, tab=0, update_tabs=True):
if update_tabs or self.tabs is None:
self.get_tabs()
wsurl = self.tabs[tab]['webSocketDebuggerUrl']
self.close()
self.ws = websocket.create_connection(wsurl)
self.ws.settimeout(self.timeout)
def connect_targetID(self, targetID):
try:
wsurl = 'ws://{}:{}/devtools/page/{}'.format(self.host, self.port, targetID)
self.close()
self.ws = websocket.create_connection(wsurl)
self.ws.settimeout(self.timeout)
except:
wsurl = self.tabs[0]['webSocketDebuggerUrl']
self.ws = websocket.create_connection(wsurl)
self.ws.settimeout(self.timeout)
def close(self):
if self.ws:
self.ws.close()
# Blocking
def wait_message(self, timeout=None):
timeout = timeout if timeout is not None else self.timeout
self.ws.settimeout(timeout)
try:
message = self.ws.recv()
except:
return None
finally:
self.ws.settimeout(self.timeout)
return json.loads(message)
# Blocking
def wait_event(self, event, timeout=None):
timeout = timeout if timeout is not None else self.timeout
start_time = time.time()
messages = []
matching_message = None
while True:
now = time.time()
if now - start_time > timeout:
break
try:
message = self.ws.recv()
print('<<< event: ' + message)
parsed_message = json.loads(message)
messages.append(parsed_message)
if 'method' in parsed_message and parsed_message['method'] == event:
matching_message = parsed_message['params']
break
except:
break
return matching_message, messages
# Blocking
def wait_result(self, result_id, timeout=None):
timeout = timeout if timeout is not None else self.timeout
start_time = time.time()
messages = []
matching_result = None
while True:
now = time.time()
if now - start_time > timeout:
break
try:
message = self.ws.recv()
print('<<< receive: ' + message)
parsed_message = json.loads(message)
messages.append(parsed_message)
if ('result' in parsed_message or 'error' in parsed_message) and parsed_message['id'] == result_id:
matching_result = parsed_message
break
except:
break
return matching_result, messages
# Non Blocking
def pop_messages(self):
messages = []
self.ws.settimeout(0)
while True:
try:
message = self.ws.recv()
messages.append(json.loads(message))
except:
break
self.ws.settimeout(self.timeout)
return messages
def __getattr__(self, attr):
genericelement = GenericElement(attr, self)
self.__setattr__(attr, genericelement)
return genericelement