Selenium4自动化测试5--控件获取数据--ALERT弹窗、Confirm弹窗、Prompt弹窗
系列导航
一、Selenium4自动化测试1--Chrome浏览器和chromedriver
二、Selenium4自动化测试2--元素定位By.ID,By.CLASS_NAME,By.TAG_NAME
四、jSelenium4自动化测试4--元素定位By.XPATH,元素定位最佳顺序
五、Selenium4自动化测试5--控件获取数据--ALERT弹窗、Confirm弹窗、Prompt弹窗
六、Selenium4自动化测试6--控件获取数据--下拉框级联选择、checkbox选择、时间选择器
七、Selenium4自动化测试7--控件获取数据--radio单选框、select下拉框选择、iframe
八、Selenium4自动化测试8--控件获取数据--上传、下载、https和切换分页
1-ALERT弹窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import time from selenium import webdriver from selenium.webdriver.common.by import By # 定义一个driver的变量,用来接收实例化后的浏览器 # 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题 chrome_location = r 'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe' options = webdriver.ChromeOptions() options.binary_location = chrome_location driver = webdriver.Chrome(options = options) # 使用get方法,访问网址 driver.get( "https://sahitest.com/demo/alertTest.htm" ) driver.find_element(By.NAME, 'b1' ).click() time.sleep( 1 ) # 使用aLert.text获取弹框的文字 print (driver.switch_to.alert.text) #点击确定 driver.switch_to.alert.accept() time.sleep( 3 ) driver.quit() |
2-Confirm弹窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import time from selenium.webdriver.support.select import Select #pip install selenium from selenium import webdriver from selenium.webdriver.common.by import By # 定义一个driver的变量,用来接收实例化后的浏览器 # 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题 chrome_location = r 'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe' options = webdriver.ChromeOptions() options.binary_location = chrome_location driver = webdriver.Chrome(options = options) # 使用get方法,访问网址 driver.get( "https://sahitest.com/demo/confirmTest.htm" ) driver.find_element(By.NAME, 'b1' ).click() time.sleep( 1 ) # 使用aLert.text获取弹框的文字 print (driver.switch_to.alert.text) #点击确定 #driver.switch_to.alert.accept() #点击取消 driver.switch_to.alert.dismiss() time.sleep( 3 ) driver.quit() |
3-Prompt弹窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import time from selenium import webdriver from selenium.webdriver.common.by import By # 定义一个driver的变量,用来接收实例化后的浏览器 # 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题 chrome_location = r 'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe' options = webdriver.ChromeOptions() options.binary_location = chrome_location driver = webdriver.Chrome(options = options) # 使用get方法,访问网址 driver.get( "https://sahitest.com/demo/promptTest.htm" ) driver.find_element(By.NAME, 'b1' ).click() time.sleep( 1 ) driver.switch_to.alert.send_keys( "你好" ) time.sleep( 1 ) #点击确定 driver.switch_to.alert.accept() time.sleep( 3 ) driver.quit() |
1、源码获取方式(免费):
(1)登录-注册:http://resources.kittytiger.cn/
(2)签到获取积分
(3)搜索:it白话-seleniume-控件操作
获取网盘地址获得源码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2023-05-10 mysql 字段逗号分割行转列操作