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

点此获取更多相关资料

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

原文链接: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()
相关推荐
山顶夕景7 分钟前
【VLM】结合Python沙箱的以图思辨S1-VL模型
python·大模型·llm·agent·多模态·vlm
和小潘一起学AI13 分钟前
Python导入私有模块(企业级方案)
开发语言·python
2401_8714928525 分钟前
Vue.js计算属性computed依赖追踪与副作用函数effect关联机制
jvm·数据库·python
qq_2837200526 分钟前
本地大模型部署全教程:Python 低成本调用开源 AI 模型
人工智能·python·开源
2401_8822737229 分钟前
SQL如何快速提取分组中最晚时间点数据_结合窗口函数实现
jvm·数据库·python
小何code29 分钟前
【Python零基础入门】第4篇:Python变量与数据类型详解
开发语言·python
卷心菜狗41 分钟前
Python进阶--生成器(Generator)
python
2301_814809861 小时前
如何用 cookie 的 HttpOnly 与 Secure 属性防范 XSS 攻击
jvm·数据库·python
李松桃1 小时前
实战:手刃豆瓣电影TOP250
python·爬虫实战·requests·re
m0_515098421 小时前
如何用 Object.keys 与 getOwnPropertyNames 遍历键名
jvm·数据库·python