弹框的定位:alert 弹框
分类:警告型弹框, 确认型弹框,输入型弹框 等
(1)确认型弹框
定位步骤
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
dr=webdriver.Chrome()
url=r"file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/enter.html"
dr.get(url)
sleep(2)
dr.find_element_by_class_name(“alert”).click()
sleep(2)
alert=dr.switch_to.alert
sleep(2)
#alert.accept() #确定
alert.dismiss() #取消
===============================================
(2)输入型弹框:
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
dr=webdriver.Chrome()
url=r"file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/prompt.html"
dr.get(url)
sleep(2)
dr.find_element_by_class_name(“alert”).click()
sleep(2)
alert=dr.switch_to.alert
sleep(2)
alert.send_keys(“hz003”)
alert.accept() #确定
#alert.dismiss() #取消
截图
(3)上传文件弹框()
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
dr=webdriver.Chrome()
url=r"file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/upload_file.html"
dr.get(url)
s=dr.find_element_by_id(“file”)
s.send_keys(r"D:\安装包\临时文件\hz.txt")
(4)alert弹框
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
dr=webdriver.Chrome()
url=r"file:///E:/dcs/two/selenium%EF%BC%888%EF%BC%89/html%E5%BC%B9%E6%A1%86/alert%E5%BC%B9%E6%A1%86.html"
dr.get(url)
sleep(2)
alert=dr.switch_to.alert
sleep(2)
alert.accept() #确定
sleep(2)
alert.accept() #确定
#alert.dismiss() #取消