在WebDriver中处理JavaScript所生成的alert、confirm以及prompt十分简单,具体做法是使用 switch_to.alert() 方法定位到 alert/confirm/prompt,然后使用text/accept/dismiss/send_keys等方法进行操作。
- text:返回 alert/confirm/prompt 中的文字信息
- accept():接受现有警告框
- dismiss():解散现有警告框
- send_keys(keysToSend):发送文本至警告框
Javascript弹窗是不能通过前端工具对其进行定位的,此时可以通过switch_to_alert()方法接收弹窗。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://www.baidu.com')
# 鼠标悬停至“设置”链接
link = driver.find_element_by_link_text('设置')
ActionChains(driver).move_to_element(link).perform()
# 打开搜索设置
driver.find_element_by_link_text("搜索设置").click()
# 保存设置
time.sleep(2)
# driver.find_element_by_class_name("prefpanelgo").click()
driver.find_element_by_xpath("//a[@class='prefpanelgo']").click()
# 接受警告框
driver.switch_to.alert.accept()
driver.quit()
通过switch_to_alert()方法获取当前页面上的警告框,并使用accept()方法接受警告框。