None

生命就像一条蜿蜒的河流

导航

selenium学习笔记08-selenium处理弹窗

selenium处理弹窗

页面上的弹窗有三种:

  • alert:用来提示
  • confirm:用来确认
  • prompt:输入内容

 

代码如下:

from selenium import webdriver
from time import sleep
import os
class TestCase(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        path = os.path.dirname(os.path.abspath(__file__))
        file_path = 'file:///' + path + '/test_alert.html'
        self.driver.get(file_path)

    def test_alert(self):
        self.driver.find_element_by_id('alert').click()
        # 切换到alert
        alert = self.driver.switch_to.alert
        print(alert.text)
        sleep(3)
        alert.accept()
    def test_confirm(self):
        self.driver.find_element_by_id('confirm').click()
        confirm = self.driver.switch_to.alert
        print(confirm.text)
        # confirm.accept()
        #
        sleep(3)
        confirm.dismiss()

    def test_prompt(self):
        self.driver.find_element_by_id('prompt').click()
        sleep(2)
        prompt = self.driver.switch_to.alert
        print(prompt.text)
        sleep(2)
        prompt.accept()
        sleep(5)


if __name__ == '__main__':
    case = TestCase()
    # case.test_alert()
    # case.test_confirm()
    case.test_prompt()
    case.driver.quit()

 

posted on 2021-02-26 09:54  我睡着了  阅读(137)  评论(0编辑  收藏  举报