警告框处理
javascript 生成的弹窗 有 alert、 confirm、prompt, 使用 switch_to_alert方法定位到alert/confirm/prompt,然后使用 text/accept/dismiss/send_keys 等方法操作。
- text: 返回alert/confirm/prompt 中的文字信息
- accept():接受现有警告框
- dismiss():解除现有警告框
- send_keys() :发送文本至警告框
from selenium import webdriver #要想调用键盘按键操作需要引入keys包 from selenium.webdriver.common.keys import Keys # from selenium.webdriver.commom.by import By from selenium.webdriver.common.action_chains import ActionChains def switch_alert(): browser = webdriver.Firefox() browser.implicitly_wait(5) browser.get("http://www.baidu.com") #鼠标悬停至 设置 链接 link=browser.find_element_by_xpath('//*[@id="s-usersetting-top"]') ActionChains(browser).move_to_element(link).perform() #打开搜索设置 browser.find_element_by_link_text(u'搜索设置').click() #保存设置 browser.find_element_by_class_name('prefpanelgo').click() #接受警告框 browser.switch_to_alert().accept() browser.quit()