| 1142 | raise WebDriverException("You can only set the orientation to 'LANDSCAPE' and 'PORTRAIT'") |
| 1143 | |
| 1144 | def start_devtools(self) -> tuple[Any, WebSocketConnection]: |
| 1145 | global cdp |
| 1146 | import_cdp() |
| 1147 | if self.caps.get("se:cdp"): |
| 1148 | ws_url = self.caps.get("se:cdp") |
| 1149 | cdp_version = self.caps.get("se:cdpVersion") |
| 1150 | if cdp_version is None: |
| 1151 | raise WebDriverException("CDP version not found in capabilities") |
| 1152 | version = cdp_version.split(".")[0] |
| 1153 | else: |
| 1154 | version, ws_url = self._get_cdp_details() |
| 1155 | |
| 1156 | if not ws_url: |
| 1157 | raise WebDriverException("Unable to find url to connect to from capabilities") |
| 1158 | |
| 1159 | if cdp is None: |
| 1160 | raise WebDriverException("CDP module not loaded") |
| 1161 | |
| 1162 | self._devtools = cdp.import_devtools(version) |
| 1163 | if self._websocket_connection: |
| 1164 | return self._devtools, self._websocket_connection |
| 1165 | if self.caps["browserName"].lower() == "firefox": |
| 1166 | raise RuntimeError("CDP support for Firefox has been removed. Please switch to WebDriver BiDi.") |
| 1167 | if not isinstance(self.command_executor, RemoteConnection): |
| 1168 | raise WebDriverException("command_executor must be a RemoteConnection instance for CDP support") |
| 1169 | self._websocket_connection = WebSocketConnection( |
| 1170 | ws_url, |
| 1171 | self.command_executor.client_config.websocket_timeout, |
| 1172 | self.command_executor.client_config.websocket_interval, |
| 1173 | ) |
| 1174 | targets = self._websocket_connection.execute(self._devtools.target.get_targets()) |
| 1175 | for target in targets: |
| 1176 | if target.target_id == self.current_window_handle: |
| 1177 | target_id = target.target_id |
| 1178 | break |
| 1179 | session = self._websocket_connection.execute(self._devtools.target.attach_to_target(target_id, True)) |
| 1180 | self._websocket_connection.session_id = session |
| 1181 | return self._devtools, self._websocket_connection |
| 1182 | |
| 1183 | @asynccontextmanager |
| 1184 | async def bidi_connection(self): |