WebDriver中处理JS所生成的alert、confirm、promot是很简单的。具体思路是使用switch_to_alert()方法定位到alert/confirm/promot。然后使用text/accept/dismiss/send_keys按需要进行操作。
下面是模拟百度首页的搜索设置。
第一次运行代码hello.py
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("http://www.baidu.com/")
#打开搜索设置
driver.find_element_by_css_selector("#u1 > a.pf").click()
driver.find_element_by_link_text(u"搜索设置").click()
time.sleep(3)
#点击保存设置
driver.find_element_by_css_selector("#gxszButton > a.prefpanelgo").click()