Path: blob/trunk/py/selenium/webdriver/common/alert.py
1864 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.16"""The Alert implementation."""1718from selenium.webdriver.common.utils import keys_to_typing19from selenium.webdriver.remote.command import Command202122class Alert:23"""Allows to work with alerts.2425Use this class to interact with alert prompts. It contains methods for dismissing,26accepting, inputting, and getting text from alert prompts.2728Accepting / Dismissing alert prompts::2930Alert(driver).accept()31Alert(driver).dismiss()3233Inputting a value into an alert prompt::3435name_prompt = Alert(driver)36name_prompt.send_keys("Willian Shakesphere")37name_prompt.accept()383940Reading a the text of a prompt for verification::4142alert_text = Alert(driver).text43self.assertEqual("Do you wish to quit?", alert_text)44"""4546def __init__(self, driver) -> None:47"""Creates a new Alert.4849:Args:50- driver: The WebDriver instance which performs user actions.51"""52self.driver = driver5354@property55def text(self) -> str:56"""Gets the text of the Alert."""57return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]5859def dismiss(self) -> None:60"""Dismisses the alert available."""61self.driver.execute(Command.W3C_DISMISS_ALERT)6263def accept(self) -> None:64"""Accepts the alert available.6566:Usage:67::6869Alert(driver).accept() # Confirm a alert dialog.70"""71self.driver.execute(Command.W3C_ACCEPT_ALERT)7273def send_keys(self, keysToSend: str) -> None:74"""Send Keys to the Alert.7576:Args:77- keysToSend: The text to be sent to Alert.78"""79self.driver.execute(Command.W3C_SET_ALERT_VALUE, {"value": keys_to_typing(keysToSend), "text": keysToSend})808182