Three types of processing switch_to.alert selenium popup box

Three kinds of pop-up box:

1, alert (a button)

2, confirm (two confirmed, canceled)

3, prompt (two buttons input box +)

4, switching to playing block: switch_to_alert () 

 

Method playing box:

1 text box text obtaining bomb

2 accept () Confirm

3 dimiss () Cancel

4 send_keys () prompt character input box shells. The other two will complain

Sample code is as follows:

from selenium import webdriver
import time

dr=webdriver.Firefox()
dr.get("https://www.baidu.com")

#隐藏的元素需显示出来才能操作
setmenu = dr.find_element_by_link_text("设置")
webdriver.ActionChains(dr).move_to_element(setmenu).perform()
dr.find_element_by_link_text('搜索设置').click()
time.sleep(3)

#未隐藏的元素可以直接选取;注释掉的是通过父级元素找后代也可以
#nrset = dr.find_element_by_id("nr")
#nrset.find_element_by_css_selector("option[value='50']").click()
dr.find_element_by_css_selector("option[value='50']").click()

#弹出框确认 三种弹出框alert(一个按钮),confirm(两个按钮),prompt(两个按钮+输入框)
dr.find_element_by_link_text("保存设置").click()
print(dr.switch_to_alert().text)
dr.switch_to_alert().accept() #确认
#dr.switch_to_alert().dismiss() #取消
#dr.switch_to_alert().send_keys("只对prompt有效") #在弹出框输入内容

time.sleep(2)
dr.quit()

 

Guess you like

Origin blog.csdn.net/chang995196962/article/details/89641100