在webdriver中处理alert、confirm、prompt十分简单,具体做法是使用switch_to.alert()
方法定位alert、confirm、prompt,然后使用
text:返回警告框中的文字信息
accept:接受现有警告框
dismiss:取消现有警告框
send_keys:发送文本到警告框
等方法进行操作
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_xpath("//*[@id='gxszButton']").click()
time.sleep(2)
# 接受警告框
driver.switch_to.alert.accept()
driver.quit()