1. 程式人生 > >Selenium_Python接口-Alert類

Selenium_Python接口-Alert類

rac conf allow orm getting basic exe 對話 __init__

Alert類的路徑:from selenium.webdriver.common.alert import Alert

Alert類主要是一些對彈出框的操作,如:獲取屬性、確認、取消等

接口內容:

from selenium.webdriver.remote.command import Command


class Alert(object):
"""
Allows to work with alerts.

Use this class to interact with alert prompts. It contains methods for dismissing,
accepting, inputting, and getting text from alert prompts.

Accepting / Dismissing alert prompts::

Alert(driver).accept()
Alert(driver).dismiss()

Inputting a value into an alert prompt:

name_prompt = Alert(driver)
name_prompt.send_keys("Willian Shakesphere")
name_prompt.accept()


Reading a the text of a prompt for verification:

alert_text = Alert(driver).text
self.assertEqual("Do you wish to quit?", alert_text)

"""

def __init__(self, driver):
"""
Creates a new Alert.

:Args:
- driver: The WebDriver instance which performs user actions.
"""
self.driver = driver

@property
def text(self):
"""
Gets the text of the Alert.
屬性:獲取彈出框的內容
@property 表示用屬性調用
"""
return self.driver.execute(Command.GET_ALERT_TEXT)["value"]

def dismiss(self):
"""
Dismisses the alert available.
不同意彈出框的內容
"""
self.driver.execute(Command.DISMISS_ALERT)

def accept(self):
"""
Accepts the alert available.
確認彈出框的內容

Usage::用法
Alert(driver).accept() # Confirm a alert dialog.
"""
self.driver.execute(Command.ACCEPT_ALERT)

def send_keys(self, keysToSend):
"""
Send Keys to the Alert.
發送內容到彈出框【適用於帶輸入的彈出框】

:Args:參數解釋
- keysToSend: The text to be sent to Alert.
"""
self.driver.execute(Command.SET_ALERT_VALUE, {‘text‘: keysToSend})

def authenticate(self, username, password):
"""
Send the username / password to an Authenticated dialog (like with Basic HTTP Auth).
Implicitly ‘clicks ok‘
發送的用戶名/密碼認證對話框(如基本的HTTP認證)。‘點擊確定‘
Usage::
driver.switch_to.alert.authenticate(‘cheese‘, ‘secretGouda‘)

:Args:
-username: string to be set in the username section of the dialog
-password: string to be set in the password section of the dialog
"""
self.driver.execute(Command.SET_ALERT_CREDENTIALS, {‘username‘:username, ‘password‘:password})
self.accept()

Selenium_Python接口-Alert類