Skip to content

Commit 0ac2b0c

Browse files
committed
support optional accept_insecure_certs and proxy params
1 parent 84adc0a commit 0ac2b0c

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

py/selenium/webdriver/common/bidi/browser.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
# under the License.
1717

1818

19+
from typing import Optional
20+
1921
from selenium.webdriver.common.bidi.common import command_builder
22+
from selenium.webdriver.common.proxy import Proxy
2023

2124

2225
class ClientWindowState:
@@ -182,14 +185,27 @@ class Browser:
182185
def __init__(self, conn):
183186
self.conn = conn
184187

185-
def create_user_context(self) -> str:
188+
def create_user_context(self, accept_insecure_certs: Optional[bool] = None, proxy: Optional[Proxy] = None) -> str:
186189
"""Creates a new user context.
187190
191+
Parameters:
192+
-----------
193+
accept_insecure_certs: Optional flag to accept insecure TLS certificates
194+
proxy: Optional proxy configuration for the user context
195+
188196
Returns:
189197
-------
190198
str: The ID of the created user context.
191199
"""
192-
result = self.conn.execute(command_builder("browser.createUserContext", {}))
200+
params = {}
201+
202+
if accept_insecure_certs is not None:
203+
params["acceptInsecureCerts"] = accept_insecure_certs
204+
205+
if proxy is not None:
206+
params["proxy"] = proxy.to_bidi_dict()
207+
208+
result = self.conn.execute(command_builder("browser.createUserContext", params))
193209
return result["userContext"]
194210

195211
def get_user_contexts(self) -> list[str]:

py/selenium/webdriver/common/proxy.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,39 @@ def to_capabilities(self):
325325
if attr_value:
326326
proxy_caps[proxy] = attr_value
327327
return proxy_caps
328+
329+
def to_bidi_dict(self):
330+
"""Convert proxy settings to BiDi format.
331+
332+
Returns:
333+
-------
334+
dict: Proxy configuration in BiDi format.
335+
"""
336+
proxy_type = self.proxyType["string"].lower()
337+
result = {"proxyType": proxy_type}
338+
339+
if proxy_type == "manual":
340+
if self.httpProxy:
341+
result["httpProxy"] = self.httpProxy
342+
if self.sslProxy:
343+
result["sslProxy"] = self.sslProxy
344+
if self.socksProxy:
345+
result["socksProxy"] = self.socksProxy
346+
if self.socksVersion is not None:
347+
result["socksVersion"] = self.socksVersion
348+
if self.noProxy:
349+
# Convert comma-separated string to list
350+
if isinstance(self.noProxy, str):
351+
result["noProxy"] = [host.strip() for host in self.noProxy.split(",") if host.strip()]
352+
elif isinstance(self.noProxy, list):
353+
if not all(isinstance(h, str) for h in self.noProxy):
354+
raise TypeError("no_proxy list must contain only strings")
355+
result["noProxy"] = self.noProxy
356+
else:
357+
raise TypeError("no_proxy must be a comma-separated string or a list of strings")
358+
359+
elif proxy_type == "pac":
360+
if self.proxyAutoconfigUrl:
361+
result["proxyAutoconfigUrl"] = self.proxyAutoconfigUrl
362+
363+
return result

0 commit comments

Comments
 (0)