Skip to content

Commit f122bd3

Browse files
committed
add unhandled_prompt_behavior param for create_user_context
1 parent ef6c472 commit f122bd3

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

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

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

1818

19-
from typing import Optional
19+
from typing import Any, Dict, Optional
2020

2121
from selenium.webdriver.common.bidi.common import command_builder
22+
from selenium.webdriver.common.bidi.session import UserPromptHandler
2223
from selenium.webdriver.common.proxy import Proxy
2324

2425

@@ -185,26 +186,35 @@ class Browser:
185186
def __init__(self, conn):
186187
self.conn = conn
187188

188-
def create_user_context(self, accept_insecure_certs: Optional[bool] = None, proxy: Optional[Proxy] = None) -> str:
189+
def create_user_context(
190+
self,
191+
accept_insecure_certs: Optional[bool] = None,
192+
proxy: Optional[Proxy] = None,
193+
unhandled_prompt_behavior: Optional[UserPromptHandler] = None,
194+
) -> str:
189195
"""Creates a new user context.
190196
191197
Parameters:
192198
-----------
193199
accept_insecure_certs: Optional flag to accept insecure TLS certificates
194200
proxy: Optional proxy configuration for the user context
201+
unhandled_prompt_behavior: Optional configuration for handling user prompts
195202
196203
Returns:
197204
-------
198205
str: The ID of the created user context.
199206
"""
200-
params = {}
207+
params: Dict[str, Any] = {}
201208

202209
if accept_insecure_certs is not None:
203210
params["acceptInsecureCerts"] = accept_insecure_certs
204211

205212
if proxy is not None:
206213
params["proxy"] = proxy.to_bidi_dict()
207214

215+
if unhandled_prompt_behavior is not None:
216+
params["unhandledPromptBehavior"] = unhandled_prompt_behavior.to_dict()
217+
208218
result = self.conn.execute(command_builder("browser.createUserContext", params))
209219
return result["userContext"]
210220

py/selenium/webdriver/common/bidi/session.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,91 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from typing import Dict, Optional
19+
1820
from selenium.webdriver.common.bidi.common import command_builder
1921

2022

23+
class UserPromptHandlerType:
24+
"""Represents the behavior of the user prompt handler."""
25+
26+
ACCEPT = "accept"
27+
DISMISS = "dismiss"
28+
IGNORE = "ignore"
29+
30+
VALID_TYPES = {ACCEPT, DISMISS, IGNORE}
31+
32+
33+
class UserPromptHandler:
34+
"""Represents the configuration of the user prompt handler."""
35+
36+
def __init__(
37+
self,
38+
alert: Optional[str] = None,
39+
before_unload: Optional[str] = None,
40+
confirm: Optional[str] = None,
41+
default: Optional[str] = None,
42+
file: Optional[str] = None,
43+
prompt: Optional[str] = None,
44+
):
45+
"""Initialize UserPromptHandler.
46+
47+
Parameters:
48+
-----------
49+
alert: Handler type for alert prompts
50+
before_unload: Handler type for beforeUnload prompts
51+
confirm: Handler type for confirm prompts
52+
default: Default handler type for all prompts
53+
file: Handler type for file picker prompts
54+
prompt: Handler type for prompt dialogs
55+
56+
Raises:
57+
------
58+
ValueError: If any handler type is not valid
59+
"""
60+
for field_name, value in [
61+
("alert", alert),
62+
("before_unload", before_unload),
63+
("confirm", confirm),
64+
("default", default),
65+
("file", file),
66+
("prompt", prompt),
67+
]:
68+
if value is not None and value not in UserPromptHandlerType.VALID_TYPES:
69+
raise ValueError(
70+
f"Invalid {field_name} handler type: {value}. Must be one of {UserPromptHandlerType.VALID_TYPES}"
71+
)
72+
73+
self.alert = alert
74+
self.before_unload = before_unload
75+
self.confirm = confirm
76+
self.default = default
77+
self.file = file
78+
self.prompt = prompt
79+
80+
def to_dict(self) -> Dict[str, str]:
81+
"""Convert the UserPromptHandler to a dictionary for BiDi protocol.
82+
83+
Returns:
84+
-------
85+
Dict[str, str]: Dictionary representation suitable for BiDi protocol
86+
"""
87+
result = {}
88+
if self.alert is not None:
89+
result["alert"] = self.alert
90+
if self.before_unload is not None:
91+
result["beforeUnload"] = self.before_unload
92+
if self.confirm is not None:
93+
result["confirm"] = self.confirm
94+
if self.default is not None:
95+
result["default"] = self.default
96+
if self.file is not None:
97+
result["file"] = self.file
98+
if self.prompt is not None:
99+
result["prompt"] = self.prompt
100+
return result
101+
102+
21103
class Session:
22104
def __init__(self, conn):
23105
self.conn = conn

0 commit comments

Comments
 (0)