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

点此获取更多相关资料

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

原文链接: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()
相关推荐
篮子里的玫瑰3 分钟前
Python与网络爬虫——列表与元组
开发语言·爬虫·python
knight_9___7 分钟前
RAG面试篇8
人工智能·python·面试·agent·rag
2301_796588508 分钟前
Go语言如何压缩文件_Go语言gzip压缩教程【基础】
jvm·数据库·python
m0_6178814210 分钟前
c++如何通过重定向rdbuf来捕获第三方库的日志输出到文件【详解】
jvm·数据库·python
Greyson119 分钟前
mysql查询执行过程中如何追踪耗时_使用PROFILE分析指令周期
jvm·数据库·python
Anesthesia丶22 分钟前
Qwen2.5-1.5b 模型部署与LORA训练笔记
pytorch·python·lora·llm·qwen·vllm
解救女汉子24 分钟前
CSS如何实现水平垂直居中效果_利用flex布局的justify-content与align-items
jvm·数据库·python
2301_7735536225 分钟前
CSS如何解决栅格重叠问题_使用Grid-area明确划分元素占位
jvm·数据库·python
覆东流29 分钟前
第6天:python综合练习——制作简易计算器
开发语言·后端·python
步辞32 分钟前
JavaScript中Symbol-keyFor检索全局符号键名逻辑
jvm·数据库·python