解决selenium的“can‘t access dead object”错误

目录

问题描述

原因

解决方法

示例代码

资料获取方法


问题描述

在python执行过程中,提示selenium.common.exceptions.WebDriverException: Message: TypeError: can't access dead object

原因

原因是代码中用到了frame,获取元素前需要切换到frame才能定位到元素,否则无法定位到元素

解决方法

python 复制代码
import time
from selenium import webdriver
 
driver = webdriver.Firefox()
 
driver.get('https://pay.xunlei.com/')
 
driver.switch_to.frame('loginIframe')
 
driver.find_element_by_xpath(".//*[@id='al_u']").send_keys('***')
 
driver.find_element_by_xpath(".//*[@id='al_p']").send_keys('***')
 
driver.find_element_by_xpath(".//*[@id='al_submit']").click()
 
time.sleep(3)
 
driver.switch_to.default_content()  # 退出frame,没有这一句后续的元素定位会出错
 
driver.find_element_by_xpath(".//*/a[text()='页游充值']").click()
 
driver.get_screenshot_as_file('ss.png')
 
time.sleep(1)
 
driver.quit()
复制代码
在查找元素前加一句driver.switch_to_default_content() 退出frame

示例代码


资料获取方法

【留言777】

各位想获取源码等教程资料的朋友请 点赞 + 评论 + 收藏 ,三连!

三连之后我会在评论区挨个私信发给你们~

相关推荐
阿巴~阿巴~18 小时前
测试用例设计万能公式:功能到安全
软件测试·测试
依旧很淡定2 天前
Selenium(Python)创建Chrome浏览器实例
chrome·python·selenium
小熊出擊2 天前
【pytest】finalizer 执行顺序:FILO 原则
python·测试工具·单元测试·pytest
云闲不收2 天前
接口请求工具对比 apifox apipost swagger postman等
测试工具·postman
sitellla2 天前
Testify Go测试工具包入门教程
git·测试工具·其他·golang
我的xiaodoujiao2 天前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 9--基础知识 5--常用函数 3
前端·python·测试工具·ui
可可南木3 天前
ICT 数字测试原理 8 - -VCL 的测试参数
开发语言·功能测试·测试工具·pcb工艺
加油20193 天前
爬虫框架: selenium API使用介绍
爬虫·selenium·测试工具
shelter -唯3 天前
基于selenium库的爬虫实战:京东手机数据爬取
爬虫·python·selenium
li星野4 天前
打工人日报#20251005
笔记·程序人生·fpga开发·学习方法