报错“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. numpy高级函数:where与extract

    numpy高级函数:where与extract 1.numpy.where()函数,此函数返回数组中满足某个条件的元素的索引: import numpy as np x = np.array([[1, ...

  2. Hive的原生部署方式

    一.Hive的部署 1.官方文档 https://cwiki.apache.org/confluence/display/Hive/GettingStarted 2.前提条件 需要安装JDK1.7之上 ...

  3. Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(例如顺序:1、初始化spring容器,2、初始化线程池,3、加载业务代码,将数据库中数据加载到内存中)

    最近公司要做功能迁移,原来的后台使用的Netty,现在要迁移到在uap上,也就是说所有后台的代码不能通过netty写的加载顺序加载了. 问题就来了,怎样让迁移到tomcat的代码按照原来的加载顺序进行 ...

  4. 使用 Visual Studio Code 进行 Laravel 开发(转)

    转自:https://laravelacademy.org/post/8016.html 关于 Laravel 代码开发工具大家各有所好,大部分应该在用 PHPStorm,Sublime 也有很多粉丝 ...

  5. 卸载重装ngin的问题解决方案

    1,卸载nginx不保留配置文件 $ sudo apt-get --purge remove nginx 2,卸载自动安装且不再需要的依赖包 $ sudo apt-get autoremove 3,卸 ...

  6. 清北学堂例题 LUOGU2523【HAOI2011】problem c

    题目描述 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1 ...

  7. mysql导入导出无权限

    error:The MySQL server is running with the --secure-file-priv option so it cannot execute this state ...

  8. 比较 CEILING 和 FLOOR

    CEILING 函数返回大于或等于所给数字表达式的最小整数. FLOOR 函数返回小于或等于所给数字表达式的最大整数. 例如,对于数字表达式  12.9273,CEILING 将返回 13,FLOOR ...

  9. python 文件夹递归

    import ospath = "F:/new" #文件夹目录datas = []def eachFile(filepath): fileNames = os.listdir(fi ...

  10. MQTT 协议学习:Retained(保留消息) 与 LWT(最后遗嘱)

    背景导入 让我们来看一下这个场景: 你有一个温度传感器,它每三个小时向一个 Topic 发布当前的温度.那么问题来了,有一个新的订阅者在它刚刚发布了当前温度之后订阅了这个主题,那么这个订阅端什么时候能 ...