1 # -*- coding:utf-8 -*- 2 """ 3 js弹框处理 4 """ 5 from selenium import webdriver 6 7 driver = webdriver.Firefox() 8 driver.get(r'F:电子书seleniumSelenium Testing Tools Cookbook(中文)demoalert.html') 9 10 driver.find_element_by_id('confirm').click() 11 12 confirmBox = driver.switch_to_alert() 13 print confirmBox.text 14 15 #点击确定按钮 16 #confirmBox.accept() 17 18 #点击取消按钮 19 confirmBox.dismiss() 20 21 #获取后,页面文本 22 print driver.find_element_by_css_selector('span').text
1 # -*- coding:utf-8 -*- 2 """ 3 输入提示框处理 4 """ 5 6 from selenium import webdriver 7 8 driver = webdriver.Firefox() 9 driver.get(r'F:电子书seleniumSelenium Testing Tools Cookbook(中文)demoalert.html') 10 11 driver.find_element_by_id('prompt').click() 12 promptAlert = driver.switch_to_alert() 13 print promptAlert.text 14 #输入数据 15 #promptAlert.send_keys('你很漂亮, 你真的很漂亮.') 16 #点击确定 17 #promptAlert.accept() 18 19 #print driver.find_element_by_tag_name("span").text 20 21 #点击取消按钮 22 promptAlert.dismiss() 23 #再次获取弹框 24 tempBox = driver.switch_to_alert() 25 print tempBox.text 26 tempBox.accept()