先上段demo:
from selenium import webdriver
import time
dr=webdriver.Firefox()
dr.get("https://www.baidu.com")
#隐藏的元素需显示出来才能操作
setmenu = dr.find_element_by_xpath("//*[@id="s-usersetting-top"]")
webdriver.ActionChains(dr).move_to_element(setmenu).perform()
dr.find_element_by_xpath('//*[@id="s-user-setting-menu"]/div/a[1]').click()
time.sleep(3)
#弹出框确认 三种弹出框alert(一个按钮),confirm(两个按钮),prompt(两个按钮+输入框)
time.sleep(1)
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()
弹窗有三种类型:
alert(一个按钮),confirm(两个按钮),prompt(两个按钮+输入框)
弹窗有四种方法:
1 text获取弹框文本
2 accept()确认
3 dimiss()取消
4 send_keys() prompt弹框输入字符。其他两种会报错