Skip to content

Commit 4082b77

Browse files
zhangwenqiang00lmtierney
authored andcommitted
Add executing custom command to Chrome Python bindings (#5989)
1 parent 5fad608 commit 4082b77

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

py/selenium/webdriver/chrome/remote_connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ def __init__(self, remote_server_addr, keep_alive=True):
2525
self._commands["launchApp"] = ('POST', '/session/$sessionId/chromium/launch_app')
2626
self._commands["setNetworkConditions"] = ('POST', '/session/$sessionId/chromium/network_conditions')
2727
self._commands["getNetworkConditions"] = ('GET', '/session/$sessionId/chromium/network_conditions')
28+
self._commands['executeCdpCommand'] = ('POST', '/session/$sessionId/goog/cdp/execute')

py/selenium/webdriver/chrome/webdriver.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ def set_network_conditions(self, **network_conditions):
115115
'network_conditions': network_conditions
116116
})
117117

118+
def execute_cdp_cmd(self, cmd, cmd_args):
119+
"""
120+
Execute Chrome Devtools Protocol command and get returned result
121+
122+
The command and command args should follow chrome devtools protocol domains/commands, refer to link
123+
https://chromedevtools.github.io/devtools-protocol/
124+
125+
:Args:
126+
- cmd: A str, command name
127+
- cmd_args: A dict, command args. empty dict {} if there is no command args
128+
129+
:Usage:
130+
driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': requestId})
131+
132+
:Returns:
133+
A dict, empty dict {} if there is no result to return.
134+
For example to getResponseBody:
135+
136+
{'base64Encoded': False, 'body': 'response body string'}
137+
138+
"""
139+
return self.execute("executeCdpCommand", {'cmd': cmd, 'params': cmd_args})['value']
140+
118141
def quit(self):
119142
"""
120143
Closes the browser and shuts down the ChromeDriver executable
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from selenium.webdriver import Chrome
19+
20+
21+
def test_execute_cdp_cmd():
22+
driver = Chrome()
23+
version_info = driver.execute_cdp_cmd('Browser.getVersion', {})
24+
assert isinstance(version_info, dict)
25+
assert 'userAgent' in version_info

0 commit comments

Comments
 (0)