报错“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)

最新文章

  1. LeetCode Intersection of Two Arrays II
  2. Swiper基本上使用
  3. C#连接数据库的新方法(通过web.config配置文件)
  4. openfire升级指南
  5. uva 10369
  6. WebService的发布及客户端的调用
  7. UDP包的大小与MTU
  8. Entity Framework入门教程(3)---EF中的上下文简介
  9. Spring Cloud Zuul 中文文件上传乱码
  10. 使用JSR-303进行校验
  11. The message port closed before a response was received.
  12. s6-6 TCP 连接释放
  13. UDS(ISO14229-2006) 汉译(No.7 应用层协议)
  14. 第一阶段——站立会议总结DAY08
  15. VS“当前上下文中不存在名称“ViewBag”,当前上下文不存在名称“model””-已解决
  16. 浅谈CORS
  17. c++ primer plus 第四章 课后题答案
  18. Session 活化与钝化 与tomcat钝化驱动器
  19. html 输入框只允许输入数字
  20. 路由器如何设置上网(TP-LINK)

热门文章

  1. CSS-font
  2. $('#myModal').modal('show') //显示$('#myModal').modal('hide')隐藏
  3. java关键字“static”
  4. NIO 组件Buffer
  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-envelope
  6. ACM-生化武器
  7. MQTT 协议学习:Retained(保留消息) 与 LWT(最后遗嘱)
  8. UVA - 11354 Bond(最小生成树+LCA+瓶颈路)
  9. Result Maps collection already contains value for com.xxx.x.dao.xxxMapper.Bas
  10. JavaScript中的面向对象及有关应用