当前位置: 首页 > news >正文

Selenium相关习题

任务1:打开百度,搜索“地球”
启动Chrome,打开https://www.baidu.com。
用 find_element 定位到搜索框(ID或Name),输入“地球”。
定位“百度一下”按钮,点击。
driver = webdriver.Chrome(service=Service('./chromedriver.exe')) driver.get('https://www.baidu.com') wait = WebDriverWait(driver, 10) wait.until(EC.element_to_be_clickable((By.ID, 'chat-textarea'))).send_keys('地球') driver.find_element(By.ID, 'chat-submit-button').click() input('按任意键继续...')
任务2:登录一个测试网站(45分钟)
推荐练习站: 搜“Selenium 练习网站”或直接用 https://practicetestautomation.com/practice-test-login/ (一个专门用来练登录的)。
定位用户名框、密码框、登录按钮,执行登录。
登录成功后,找到页面上的“Log out”按钮或某个成功提示文本,打印出来。
在这里你会遇到等待的痛点:登录按钮点了之后,页面会跳转,如果你不等,下一个元素就找不到。试着用显式等待解决。
driver = webdriver.Chrome(service=Service('./chromedriver.exe')) driver.get('https://practicetestautomation.com/practice-test-login/') driver.find_element(By.ID, 'username').send_keys('student') driver.find_element(By.ID, 'password').send_keys('Password123') driver.find_element(By.ID, 'submit').click() wait = WebDriverWait(driver, 10) text = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="loop-container"]/div/article/div[2]/div/div/div/a'))).text print(text) input('按任意键继续...')
任务3:带数据驱动的登录测试(升级,90分钟)
推荐练习站: 搜“Selenium 练习网站”或直接用 https://practicetestautomation.com/practice-test-login/ (一个专门用来练登录的)

创建一个CSV或Excel文件,里面有多组用户名和密码(包括正确的和错误的)。
写一个循环,从文件里读取每组数据,执行登录,然后根据预期结果(成功或失败)判断测试是否通过,并用Python的print输出结果。
这是你简历上第一个“自动化测试框架”的雏形,非常重要。

表格数据:

def get_data_from_excel(file_path): wb = openpyxl.load_workbook(file_path) sheet = wb.active data = [] for row in sheet.iter_rows(min_row=2, values_only=True): data.append(row) return data def test_login(username, password): WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys(username) driver.find_element(By.ID, 'password').send_keys(password) driver.find_element(By.ID, 'submit').click() try: WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="loop-container"]/div/article/div[2]/div/div/div/a'))) except TimeoutException: return False else: return True driver = webdriver.Chrome(service=Service('./chromedriver.exe')) driver.get('https://practicetestautomation.com/practice-test-login/') data = get_data_from_excel('./testdata.xlsx') for username, password in data: login_success = test_login(username, password) if login_success: driver.back() print(f"username = {username},password = {password}登录成功") else: print(f"username = {username},password = {password}登录失败") input('按任意键继续...')
任务4(选做,进阶):使用Page Object模式重构
把任务3转换成Page Object 设计模式。很简单,就是把“一个页面的所有元素定位和操作”封装成一个类,
测试脚本只调用类的方法,看起来特别清晰易维护。
这个模式你只要在面试中能讲出它的好处,就能秒杀很多竞争者:
好维护‌:页面元素变了,只用改对应的页面对象类,不用改所有测试用例 。
‌能复用‌:同一个页面的操作方法可以被多个测试用例重复调用,减少重复代码 。
‌看得懂‌:测试用例里全是业务语言,不像以前那样夹杂大量技术代码,逻辑更清晰
class PageLogin: #页面网址 URL = 'https://practicetestautomation.com/practice-test-login/' #元素定位器 USERNAME_INPUT = (By.ID, 'username') PASSWORD_INPUT = (By.ID, 'password') LOGIN_CLICK = (By.ID, 'submit') SUCCESS_FLAG = (By.XPATH, '//*[@id="loop-container"]/div/article/div[2]/div/div/div/a') #初始化driver def __init__(self, driver): self.driver = driver #进入网站 def open(self): self.driver.get(self.URL) #输入账户 def username_enter(self, username): WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable(self.USERNAME_INPUT)).send_keys(username) #输入密码 def password_enter(self, password): self.driver.find_element(*self.PASSWORD_INPUT).send_keys(password) #点击登录按钮 def login_click(self): self.driver.find_element(*self.LOGIN_CLICK).click() #登录 def test_login(self,username, password): self.username_enter(username) self.password_enter(password) self.login_click() try: WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable(self.SUCCESS_FLAG)) except TimeoutException: return False else: return True def get_data_from_excel(file_path): wb = openpyxl.load_workbook(file_path) sheet = wb.active data = [] for row in sheet.iter_rows(min_row=2, values_only=True): data.append(row) return data driver = webdriver.Chrome(service=Service('./chromedriver.exe')) pagelogin = PageLogin(driver) pagelogin.open() data = get_data_from_excel('testdata.xlsx') for username, password in data: result = pagelogin.test_login(username, password) if result: driver.back() print(f"username = {username},password = {password}登录成功") else: print(f"username = {username},password = {password}登录失败") input('按任意键继续...')
http://www.jsqmd.com/news/1090942/

相关文章:

  • 卷疯了!这款 macOS 神器一个顶五个:截图 + 录屏 + 取色 + 贴图 + 右键增强,还完全免费开源
  • 3分钟快速解密:RPG Maker MV资源提取工具让游戏素材轻松解锁
  • FreeRTOS源码详解(六)—— 任务切换
  • 天辛大师漫谈AI时代的境界修养,文科生的持续学习
  • 别让AI每天从零开始:一个研发老兵的Skills沉淀实操指南
  • 【Netty源码解读和权威指南】第81篇:Netty Codec框架源码解析——编解码器是如何设计的
  • dxwrapper终极指南:让Windows 10/11完美运行经典老游戏的技术方案
  • 企业文件怎么加密防泄漏?5款小白都能用的企业加密软件分享,内行人推荐
  • FreeRTOS源码详解(十一)——Alarm
  • Windows风扇控制终极指南:Fan Control如何帮你告别噪音烦恼
  • HS2-HF Patch:深度解析Honey Select 2终极增强方案的技术架构与高级应用
  • 装了这个插件,哔哩哔哩网页版真好用~
  • 软件测试面试全攻略:1000+真题解析与实战技巧
  • 程序员开国际技术会议,2026年3款英汉互译在线工具哪个实用?
  • Codex在win11下安装并设置Mimo的代理
  • Open Harmony 能力增强:main_pages.json 页面注册机制解析
  • 深耕复古不踩坑!冰雪传奇点卡版真实还原经典雪域开荒玩法
  • 终极指南:3步使用Untrunc免费修复损坏的MP4视频文件
  • Web安全实战:从文件上传到SSRF,DVWA靶场漏洞复现与防御指南
  • 笔试强训 Day 15:平方数 + 分组 + 拓扑排序
  • 【JAVA毕设源码分享】基于springboot智能垃圾分类系统的设计与实现(程序+文档+代码讲解+一条龙定制)
  • 循环的跳出
  • C语言工具的安装(DEV-C++)
  • Windows11 2026 年 6 月 23 日 — KB5095093
  • 欧洲41.5度热浪的残酷警示:技术韧性是数字基建的最后一道防线
  • 求职期间项目一直在更新,简历总是忘了改——于是我写了一个自动同步工具
  • Java21+Jenkins2.555.1简易下载安装流程
  • 提示词工程已死,Loop Engineering 称王!保姆级教程 + 项目实战
  • 笔试强训 Day 16:字符串替换 + 神奇数 + DNA 序列
  • WaveTools鸣潮工具箱终极指南:3步实现免费帧率解锁与智能抽卡管理