欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Selenium- alert -confirm -prompt三种弹窗

程序员文章站 2022-04-08 21:41:35
...
#定位alert弹窗的属性 并点击
#调用switch_to_alert.accept() 

#定位confim弹窗
调用switch_to.alert.accept()
#点击取消
switch_to.alert.dismiss()

#定位prompt弹窗 输入内容吧并确定
调用 switch_to.alert.send_kyes("")
#点击确定
switche_to.alert.accept()

代码如下

from  selenium import  webdriver
import  time

driver = webdriver.Chrome()


#alert弹窗
driver.get("http://sahitest.com/demo/alertTest.htm")
driver.find_element_by_name("b1").click()
time.sleep(3)
driver.switch_to.alert.accept()
time.sleep(3)

#confim弹窗 确定 取消
driver.get("http://sahitest.com/demo/confirmTest.htm")
driver.find_element_by_name("b1").click()
time.sleep(3)
driver.switch_to.alert.accept()
time.sleep(3)
driver.find_element_by_name("b1").click()
time.sleep(3)
driver.switch_to.alert.dismiss()
time.sleep(3)

#promot 弹窗 输入内容并 确定或取消
driver.get("http://sahitest.com/demo/promptTest.htm")
driver.find_element_by_name("b1").click()
time.sleep(3)
driver.switch_to.alert.send_keys("1123")
time.sleep(3)
driver.switch_to.alert.accept()
time.sleep(3)
driver.quit()