selumium 中 xpath获取文本、属性正确写法
2024-10-21 16:05:15
报错“The result of the xpath expression is: [object Attr]. It should be an element”
yutube爬虫动态加载,需要用到selenium-webdriver,使用过程中,首先使用
find_elements_by_xpath进行批量标签的定位选取,之后
使用find_element_by_xpath精细筛选选标签的时候出现上面错误提示,
原因是这个webdriver的定位方法和浏览器xpath不一样,不能直接定位到标签的属性
需要首先定位到webelement,之后get到属性
正确
try:
temp['host_url'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a').get_attribute('href')
except Exception as e:
print(e)
try:
temp['show_url'] = node.find_element_by_xpath('./div/ytd-thumbnail/a').get_attribute('href')
except Exception as e:
print(e)
try:
temp['title'] = node.find_element_by_xpath('./div/div/div[1]/div/h3/a').get_attribute('title')
except Exception as e:
print(e)
try:
temp['user'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a').text
except Exception as e:
错误:
try:
temp['host_url'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a/@href')
except Exception as e:
print(e)
try:
temp['show_url'] = node.find_element_by_xpath('./div/ytd-thumbnail/a/@href')
except Exception as e:
print(e)
try:
temp['title'] = node.find_element_by_xpath('./div/div/div[1]/div/h3/a/@title')
except Exception as e:
print(e)
try:
temp['user'] = node.find_element_by_xpath('./div/div/div/ytd-video-meta-block/div/div/div/yt-formatted-string/a/text()')
except Exception as e:
print(e)
最新文章
- LeetCode Intersection of Two Arrays II
- Swiper基本上使用
- C#连接数据库的新方法(通过web.config配置文件)
- openfire升级指南
- uva 10369
- WebService的发布及客户端的调用
- UDP包的大小与MTU
- Entity Framework入门教程(3)---EF中的上下文简介
- Spring Cloud Zuul 中文文件上传乱码
- 使用JSR-303进行校验
- The message port closed before a response was received.
- s6-6 TCP 连接释放
- UDS(ISO14229-2006) 汉译(No.7 应用层协议)
- 第一阶段——站立会议总结DAY08
- VS“当前上下文中不存在名称“ViewBag”,当前上下文不存在名称“model””-已解决
- 浅谈CORS
- c++ primer plus 第四章 课后题答案
- Session 活化与钝化 与tomcat钝化驱动器
- html 输入框只允许输入数字
- 路由器如何设置上网(TP-LINK)
热门文章
- CSS-font
- $('#myModal').modal('show') //显示$('#myModal').modal('hide')隐藏
- java关键字“static”
- NIO 组件Buffer
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-envelope
- ACM-生化武器
- MQTT 协议学习:Retained(保留消息) 与 LWT(最后遗嘱)
- UVA - 11354 Bond(最小生成树+LCA+瓶颈路)
- Result Maps collection already contains value for com.xxx.x.dao.xxxMapper.Bas
- JavaScript中的面向对象及有关应用