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

点此获取更多相关资料

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

原文链接: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()
相关推荐
傻啦嘿哟17 分钟前
Python实现PDF文档高效转换为HTML文件:从基础到进阶的完整指南
python·pdf·html
天下无敌笨笨熊1 小时前
ES作为向量库研究
大数据·python·elasticsearch
数据知道1 小时前
FastAPI项目:从零到一搭建一个网站导航系统
python·mysql·fastapi·python web·python项目
程序员爱钓鱼2 小时前
Python 编程实战 · 进阶与职业发展:数据分析与 AI(Pandas、NumPy、Scikit-learn)
后端·python·trae
软件开发技术深度爱好者2 小时前
Python库/包/模块管理工具
开发语言·python
程序员爱钓鱼2 小时前
Python 编程实战 · 进阶与职业发展:Web 全栈(Django / FastAPI)
后端·python·trae
郝学胜-神的一滴3 小时前
Python中一切皆对象:深入理解Python的对象模型
开发语言·python·程序人生·个人开发
烤汉堡3 小时前
Python入门到实战:post请求和响应
python·html
夫唯不争,故无尤也3 小时前
Python广播机制:张量的影分身术
开发语言·python
流浪猪头拯救地球4 小时前
利用 Python 解密 / 加密 PDF 文件
python·pdf·php