Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/alert.py
1864 views
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
"""The Alert implementation."""
18
19
from selenium.webdriver.common.utils import keys_to_typing
20
from selenium.webdriver.remote.command import Command
21
22
23
class Alert:
24
"""Allows to work with alerts.
25
26
Use this class to interact with alert prompts. It contains methods for dismissing,
27
accepting, inputting, and getting text from alert prompts.
28
29
Accepting / Dismissing alert prompts::
30
31
Alert(driver).accept()
32
Alert(driver).dismiss()
33
34
Inputting a value into an alert prompt::
35
36
name_prompt = Alert(driver)
37
name_prompt.send_keys("Willian Shakesphere")
38
name_prompt.accept()
39
40
41
Reading a the text of a prompt for verification::
42
43
alert_text = Alert(driver).text
44
self.assertEqual("Do you wish to quit?", alert_text)
45
"""
46
47
def __init__(self, driver) -> None:
48
"""Creates a new Alert.
49
50
:Args:
51
- driver: The WebDriver instance which performs user actions.
52
"""
53
self.driver = driver
54
55
@property
56
def text(self) -> str:
57
"""Gets the text of the Alert."""
58
return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
59
60
def dismiss(self) -> None:
61
"""Dismisses the alert available."""
62
self.driver.execute(Command.W3C_DISMISS_ALERT)
63
64
def accept(self) -> None:
65
"""Accepts the alert available.
66
67
:Usage:
68
::
69
70
Alert(driver).accept() # Confirm a alert dialog.
71
"""
72
self.driver.execute(Command.W3C_ACCEPT_ALERT)
73
74
def send_keys(self, keysToSend: str) -> None:
75
"""Send Keys to the Alert.
76
77
:Args:
78
- keysToSend: The text to be sent to Alert.
79
"""
80
self.driver.execute(Command.W3C_SET_ALERT_VALUE, {"value": keys_to_typing(keysToSend), "text": keysToSend})
81
82