彈窗類型:彈出框有兩種:
頁面彈出框(可定位元素能操作)----div……
Windows彈出框(不能直接定位)----alert,confirm,prompt……
一.頁面彈出框
div彈窗不需要切換iframe,元素需要二次定位,python寫法為:
driver.find_element_by_class_name("tang-pass-footerBar").find_element_by_id('TANGRAM__PSP_10__footerULoginBtn').click()
實例如下:打開百度---登錄---用戶名登錄---賬號/密碼---登錄
selenium提供switch_to.alert方法:捕獲彈出對話框(可以定位alert、confirm、prompt對話框)
switch_to.alert --定位彈出對話框
text() --獲取對話框文本值
accept() --相當於點擊“確認”
dismiss() --相當於點擊“取消”
send_keys() --輸入值(alert和confirm沒有輸入對話框,所以就不用能用了,只能使用在prompt里)
二.windows彈出框
1.定位alert彈出框
注意:獲取alert彈出框時使用 x = m.switch_to.alert,而不是 x = m.switch_to.alert(),否則會提示錯誤:TypeError: 'Alert' object is not callable(對象不能用函數形式調用)
2.定位confirm彈出框
alert =driver.switch_to.alert
alert.dismiss()
3.定位prompt彈出框
alert =driver.switch_to.alert
alert.send_keys("python")
alert.accept()