webdriver中弹框使用
2021-03-09 09:31
webdriver中弹框
1 #1:定位alert弹出框
2 #点击页面元素,触发alert弹出框
3 driver.find_element_by_xpath(‘//*[@id="alert"]‘).click()
4 time.sleep(3)
5 #等待alert弹出框可见
6 WebDriverWait(driver,20).until(EC.alert_is_present())
7
8 #从html页面切换到alert弹框
9 alert = driver.switch_to.alert
10 #获取alert的文本内容
11 print(alert.text)
12 #接受--选择“确定”
13 alert.accept()
14
15 #2:定位confirm弹出框
16 driver.find_element_by_xpath(‘//*[@id="confirm"]‘).click()
17 time.sleep(3)
18 WebDriverWait(driver,20).until(EC.alert_is_present())
19 alert =driver.switch_to.alert
20 print(alert.text)
21 # 接受--选择“取消”
22 alert.dismiss()
23
24
25 #3:定位prompt弹出框
26 driver.find_element_by_id("prompt").click()
27 time.sleep(3)
28 WebDriverWait(driver,20).until(EC.alert_is_present())
29 alert =driver.switch_to.alert
30 alert.send_keys("jaja")
31 time.sleep(5)
32 print(alert.text)
33 # alert.dismiss()
34 alert.accept()