DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<a href="javascript:alert('提示框')" id="alert">Alerta><br>
<a href="javascript:confirm('真的要删除吗?')" id="confirm">Confrima><br>
<a href="javascript:var age = prompt('请输入年龄');document.write(age)" id="prompt">Prompta><br>
body>
html>
# 弹窗处理 alert confirm prompt
# alert 用来提示
# confirm 用来确认
# prompt 输入内容
# accept() 接受
# dismiss() 取消
from selenium import webdriver
from time import sleep
import os
class Testcase(object):
def __init__(self):
self.driver = webdriver.Chrome()
path = os.getcwd()
file_path = os.path.join(path, "study04.html")
self.driver.get(file_path)
self.driver.maximize_window()
def test01(self):
self.driver.find_element_by_id("alert").click()
# 切换到alert
alert = self.driver.switch_to.alert
print(alert.text)
sleep(3)
alert.accept()
self.driver.quit()
def test02(self):
self.driver.find_element_by_id("confirm").click()
confirm = self.driver.switch_to.alert
print(confirm.text)
sleep(3)
confirm.dismiss()
def test03(self):
self.driver.find_element_by_id("prompt").click()
sleep(2)
prompt = self.driver.switch_to.alert
prompt.send_keys("20")
sleep(3)
prompt.accept()
sleep(3)
if __name__ == '__main__':
case = Testcase()
case.test03()
case.driver.quit()