前言:
现在看官们对“html弹窗alert”可能比较重视,姐妹们都想要知道一些“html弹窗alert”的相关文章。那么小编也在网上收集了一些有关“html弹窗alert””的相关知识,希望看官们能喜欢,同学们快快来了解一下吧!在测试中,有时候会遇到弹窗的问题,有的是浏览器弹窗(alert)、有的是自定义弹窗;这节我们主要来讨论一下关于浏览器弹窗和简单的自定义弹窗。
一、关于alert弹窗的方法
switch_to_alert(): 定位到alert弹窗,返回一个弹窗的对象
dismiss(): 对弹窗对象的取消操作(相当于点击弹窗上的取消按钮)
accept():对弹窗对象的确定操作(相当于点击弹窗上的确定按钮)
text:对弹窗对象,获取弹窗内的文本
send_keys(key):对弹窗对象内的输入框输入数据(如果弹窗的格式有输入框的话可以使用)
authenticate(name, pass):对于身份认证弹窗,输入用户名和密码并自动提交(一般可能会用于本地搭建的一些系统)如图:
二、alert包的源码展示
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).textself.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@propertydef text(self): """Gets the text of the Alert. """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."""if self.driver.w3c:self.driver.execute(Command.SET_ALERT_VALUE, {'value': keys_to_typing(keysToSend)})else: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'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()三、实例:w3c的alert页面
代码展示:
from selenium import webdriverfrom time import sleepdriver = webdriver.Chrome()driver.get(";) # 通过frame的name值来定位driver.switch_to_frame("i") # 点击按钮触发弹窗ele = driver.find_element_by_css_selector("body > input[type='button']")ele.click()sleep(2) # 定位到到弹窗a = driver.switch_to_alert()print(driver)# 获取弹窗的内容print(a.text) # 触发取消按钮a.dismiss()sleep(2) # 再次点击按钮触发弹窗ele.click() # 在弹窗中的输入框输入数据a.send_keys("许西城")sleep(2) # 触发确认按钮a.accept()四、普通的隐藏弹窗
平时的话,我们一般遇到的都是自定义弹窗,所以说一般不是不用到alert的,但是还是要拿出来说一下的;一般这种自定义弹窗是自定义的div层,然后是隐藏的,所以当你触发了这个弹窗后,它就会显示出来,这时我们通过正常的定位方式是可以正常定位到的。
下面就主要看一下百度的登录弹窗
代码展示:
from selenium import webdriverfrom time import sleep# 打开谷歌浏览器driver = webdriver.Chrome() # 输入网址并访问driver.get(";) # 点击登录按钮driver.find_element_by_css_selector("#u1 > a.lb").click()sleep(2) # 定位登录弹窗的输入框,并输入数据name_box = driver.find_element_by_css_selector("#TANGRAM__PSP_10__userName")name_box.send_keys("name")
标签: #html弹窗alert