1、alert弹框切换
alert=driver.switch_to.alert
- 等待alert弹框出现
EC.alert_is_present()
- 切换到alert弹框
alert=driver.switch_to.alert
- 确定alert弹框/取消alert弹框
alert.accept()#确定
alert.dismiss()#取消
代码操作如下:
1 # alert切换 2 from selenium import webdriver 3 from selenium.webdriver.support import expected_conditions as EC 4 from selenium.webdriver.support.wait import WebDriverWait 5 # 窗口切换操作 6 driver=webdriver.Chrome() 7 # 访问一个网页 8 driver.get(r"C:xxxpython自动化测试python11_web_farmworkstudentfrist_html.html") 9 # driver.maximize_window() 10 # 等待alert弹框出现 11 WebDriverWait(driver,10).until(EC.alert_is_present()) 12 # 切换到alert弹框 13 alert=driver.switch_to.alert 14 print(alert.text)#获取弹窗中的内容 15 alert.accept()#接受 16 # alert.dismiss()#拒绝
备注:web自动化中的三种切换都是使用的driver.switch_to.iframe/window/alert