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
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import ctypes
import json
class ChromeDriverException(Exception):
pass
class UnknownCommand(ChromeDriverException):
pass
class UnknownError(ChromeDriverException):
pass
class SessionNotCreatedException(ChromeDriverException):
pass
class NoSuchSession(ChromeDriverException):
pass
def _ExceptionForResponse(response):
exception_class_map = {
9: UnknownCommand,
13: UnknownError,
33: SessionNotCreatedException,
100: NoSuchSession
}
status = response['status']
msg = response['value']['message']
return exception_class_map.get(status, ChromeDriverException)(msg)
class ChromeDriver(object):
"""Starts and controls a single Chrome instance on this machine."""
def __init__(self, lib_path, chrome_binary=None):
self._lib = ctypes.CDLL(lib_path)
if chrome_binary is None:
params = {}
else:
params = {
'desiredCapabilities': {
'chromeOptions': {
'binary': chrome_binary
}
}
}
self._session_id = self._ExecuteCommand('newSession', params)['sessionId']
def _ExecuteCommand(self, name, params={}, session_id=''):
cmd = {
'name': name,
'parameters': params,
'sessionId': session_id
}
cmd_json = json.dumps(cmd)
response_data = ctypes.c_char_p()
response_size = ctypes.c_uint()
self._lib.ExecuteCommand(
ctypes.c_char_p(cmd_json),
ctypes.c_uint(len(cmd_json)),
ctypes.byref(response_data),
ctypes.byref(response_size))
response_json = ctypes.string_at(response_data, response_size.value)
self._lib.Free(response_data)
response = json.loads(response_json)
if response['status'] != 0:
raise _ExceptionForResponse(response)
return response
def _ExecuteSessionCommand(self, name, params={}):
return self._ExecuteCommand(name, params, self._session_id)['value']
def Load(self, url):
self._ExecuteSessionCommand('get', {'url': url})
def ExecuteScript(self, script, *args):
return self._ExecuteSessionCommand(
'executeScript', {'script': script, 'args': args})
def SwitchToFrame(self, id_or_name):
self._ExecuteSessionCommand('switchToFrame', {'id': id_or_name})
def SwitchToFrameByIndex(self, index):
self.SwitchToFrame(index)
def SwitchToMainFrame(self):
self.SwitchToFrame(None)
def GetTitle(self):
return self._ExecuteSessionCommand('getTitle')
def Quit(self):
"""Quits the browser and ends the session."""
self._ExecuteSessionCommand('quit')
|