软件测试/测试开发丨文件上传与弹窗处理 学习笔记

点此获取更多相关资料

本文为霍格沃兹测试开发学社学员学习笔记分享

原文链接:https://ceshiren.com/t/topic/27071

一、文件上传

  • input标签可以直接使用send_keys("文件路径/文件名")上传文件

    • ele01 = driver.find_element(By.ID,"上传按钮id")
    • ele01.send_keys("文件路径/文件名")
python 复制代码
    def test_upload_file(self):
        self.driver.get("https://image.baidu.com/")
        self.driver.find_element(By.XPATH, '//*[@class="st_camera_off"]').click()
        ele01 = self.driver.find_element(By.ID, 'stfile')
        ele01.send_keys(r"C:\Users\Chen\PycharmProjects\hogwartsCourse\UIAutoTest\UI_L2\pictures\pic01.png")
        time.sleep(5)
        print(self.driver.title)

二、弹窗处理

2.1、弹窗处理机制

  • 在页面操作时,有时会遇到JS生成的alert、confirm、prompt弹窗,我们可以使用switch_to.alert()方法定位到。

  • 操作alert常用的方法:

    • switch_to.alert():获取当前页面的弹窗
    • text:返回弹窗中的文本信息
    • accept():接受现有的弹窗
    • dismiss():解算取消现有的弹窗
    • send_keys():发送文本至弹窗
python 复制代码
    def test_accept_alert(self):
        self.driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
        self.driver.switch_to.frame("iframeResult")
        ele01 = self.driver.find_element(By.ID, "draggable")
        print(ele01.text)
        ele02 = self.driver.find_element(By.ID, "droppable")
        print(ele02.text)
        self.action.drag_and_drop(ele01, ele02).perform()
        time.sleep(3)
        self.driver.switch_to.alert.accept()
        self.driver.switch_to.parent_frame()
        self.driver.find_element(By.ID,"submitBTN").click()
相关推荐
子夜四时歌10 分钟前
Python详细安装与环境搭建
开发语言·python
Jinkxs11 分钟前
SkyWalking - Python 应用追踪:基于 skywalking-python 的埋点
开发语言·python·skywalking
大头博士先生11 分钟前
【3月考】二级Python最新真题及满分代码合集(基本操作题部分)
开发语言·python
白狐_79813 分钟前
【实战架构】一人抵一家设计公司:基于 ComfyUI + Python RPA + Photoshop 的全自动化工业制图工作流
python·photoshop·rpa
shengli72213 分钟前
Python在金融科技(FinTech)中的应用
jvm·数据库·python
xcLeigh13 分钟前
IoTDB Python原生接口全攻略:从基础读写到高级实战
开发语言·数据库·python·api·iotdb·原生接口·读写数据
User_芊芊君子14 分钟前
文科生封神!Python+AI 零门槛变现:3 天造 App,指令即收入(附脉脉 AI 沙龙干货)
开发语言·人工智能·python
MeowNeko14 分钟前
为什么说程序员重命名时电脑不要带中文?记一次python manage.py runserver时UnicodeDecodeError的原因与解决方案
人工智能·python·chatgpt·中间件·django·utf8
是Dream呀14 分钟前
2025年中秋月亮只有94.91%圆?Python告诉你真相
开发语言·python·中秋节
星辰徐哥15 分钟前
Python开发从入门到精通:异步编程与协程
开发语言·python