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

selumium 中 xpath获取文本、属性正确写法的更多相关文章

  1. Python中如何获取类属性的列表

    这篇文章主要给大家介绍了在Python中如何获取类属性的列表,文中通过示例代码介绍的很详细,相信对大家的学习或者工作具有一定的参考借鉴价值,有需要的朋友可以参考借鉴,下面来一起看看吧. 前言 最近工作 ...

  2. 使用python处理selenium中的获取文本问题

    # 获取文本 button_name = self.driver.find_element_by_id("sign_in_display").text

  3. 第92天:CSS3中颜色和文本属性

    一.颜色的表示方式 1. rgba(255,0,0,0.1)  rgba是代表Red(红色) Green(绿色) Blue(蓝色)和 Alpha透明度.虽然它有的时候被描述为一个颜色空间 新增了RGB ...

  4. 在浏览器中直接调用webservice的正确写法

    此文章针对webwork+spring+hibernate的工程,对于其他框架应该一样适用,首先在wsdd文件中找到所需webservice的名称,例如以下写法: <service name=& ...

  5. 使用python处理selenium中的获取元素属性问题

    # 获取我的订单元素class属性值 at = self.driver.find_element_by_link_text('我的订单').get_attribute('class') # 判断cla ...

  6. 使用python处理selenium中的获取元素属性

    # 获取我的订单元素class属性值 get_class_name = driver.find_element_by_link_text('我的订单').get_attribute('class') ...

  7. jquery中css获取颜色属性

    Jquery获取颜色的方法为: var color = $(元素).css("color"); alert(color); 可以看到color如这样的格式; 但是; 因为rgb(0 ...

  8. php中如何获取文件的正确路径

    以上面的图片为例子 //我们这里需要使用到 $_FILE echo "<pre>"; print_r($_FILES); echo $_SERVER['DOCUMENT ...

  9. javascript中封装获取样式属性值的兼容方法

    function getStyle(obj, attr) { if (window.getComputedStyle) { return window.getComputedStyle(obj, nu ...

随机推荐

  1. Deepctr框架代码阅读

    DeepCtr是一个简易的CTR模型框架,集成了深度学习流行的所有模型,适合学推荐系统模型的人参考. 我在参加比赛中用到了这个框架,但是效果一般,为了搞清楚原因从算法和框架两方面入手.在读代码的过程中 ...

  2. EventLoop

    文章资料来自 Node.js 事件循环机制 JS灵魂之问(下) EventLoop的中国名字叫事件循环,这个玩意真的是高深莫测,一般开发都用不到,代码只管写就行,虽然不用懂,但是面试就是要问,这对我这 ...

  3. python集成开发环境Anaconda的安装

    参考博文: anaconda在Linux下的安装 Linux下anaconda3的安装 Anaconda的安装.启用及停用的步骤 Python学习之Anaconda的使用及配置方法 Anaconda ...

  4. php 文件缓存 include vs serialize vs json_encode

    大神:http://techblog.procurios.nl/k/news/view/34972/14863/cache-a-large-array-json-serialize-or-var_ex ...

  5. mysql合并结果集

  6. CentOS7.x关闭防火墙

    使用命令:systemctl status firewalld.service 查看防火墙状态 执行后可以看到绿色字样标注的“active(running)”,说明防火墙是开启状态 使用命令:syst ...

  7. 解决d7在更高版本上运行乱码问题,或者是调用更高版本的dll

    将String类型改成WideString类型即可

  8. Codeforces Round #611 (Div. 3)

    原题面:https://codeforces.com/contest/1283 A.Minutes Before the New Year 题目大意:给定时间,问距离零点零分还有多久? 分析:注意一下 ...

  9. POJ 2142:The Balance

    The Balance Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4781   Accepted: 2092 Descr ...

  10. Set Request Header

    第1步 webpack package.json 的 scripts 里的 dev 要新增 --host 0.0.0.0 这样在浏览器里用ip地址代替localhost就能运行了 第2步 在pc浏览器 ...