其他分享
首页 > 其他分享> > selenium 弹窗处理

selenium 弹窗处理

作者:互联网

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<a href="javascript:alert('提示框')" id="alert">Alert</a><br>
<a href="javascript:confirm('真的要删除吗?')" id="confirm">Confrim</a><br>
<a href="javascript:var age = prompt('请输入年龄');document.write(age)" id="prompt">Prompt</a><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()

 

标签:__,prompt,处理,selenium,self,driver,alert,sleep,弹窗
来源: https://www.cnblogs.com/xytyyds/p/15980217.html