selumium 中 xpath获取文本、属性正确写法
报错“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获取文本、属性正确写法的更多相关文章
- Python中如何获取类属性的列表
这篇文章主要给大家介绍了在Python中如何获取类属性的列表,文中通过示例代码介绍的很详细,相信对大家的学习或者工作具有一定的参考借鉴价值,有需要的朋友可以参考借鉴,下面来一起看看吧. 前言 最近工作 ...
- 使用python处理selenium中的获取文本问题
# 获取文本 button_name = self.driver.find_element_by_id("sign_in_display").text
- 第92天:CSS3中颜色和文本属性
一.颜色的表示方式 1. rgba(255,0,0,0.1) rgba是代表Red(红色) Green(绿色) Blue(蓝色)和 Alpha透明度.虽然它有的时候被描述为一个颜色空间 新增了RGB ...
- 在浏览器中直接调用webservice的正确写法
此文章针对webwork+spring+hibernate的工程,对于其他框架应该一样适用,首先在wsdd文件中找到所需webservice的名称,例如以下写法: <service name=& ...
- 使用python处理selenium中的获取元素属性问题
# 获取我的订单元素class属性值 at = self.driver.find_element_by_link_text('我的订单').get_attribute('class') # 判断cla ...
- 使用python处理selenium中的获取元素属性
# 获取我的订单元素class属性值 get_class_name = driver.find_element_by_link_text('我的订单').get_attribute('class') ...
- jquery中css获取颜色属性
Jquery获取颜色的方法为: var color = $(元素).css("color"); alert(color); 可以看到color如这样的格式; 但是; 因为rgb(0 ...
- php中如何获取文件的正确路径
以上面的图片为例子 //我们这里需要使用到 $_FILE echo "<pre>"; print_r($_FILES); echo $_SERVER['DOCUMENT ...
- javascript中封装获取样式属性值的兼容方法
function getStyle(obj, attr) { if (window.getComputedStyle) { return window.getComputedStyle(obj, nu ...
随机推荐
- 最新版本GIT安装
版本问题,最直接的解决办法就是重新编辑安装git吧: 1. 下载:# wget -O git.zip https://github.com/git/git/archive/master.zip 2. ...
- mongodb - 关联字段
1,博客表结构 Blog.js var mongoose = require('mongoose') mongoose.connect('mongodb://localhost/test',{ us ...
- win10提示防火墙没有法更改某些设置的处理办法
一.问题发现 远程链接电脑时间发现远程链接失败 提问在“控制面板” 中打开“程序” 列表中启用“windows 防火墙” . 按照提示启用防火墙 ,发现启用或关闭页面不可编辑 二.原因是防火墙Wind ...
- 009-PHP循环输出数组成员
<?php $Cities[] = "<B>北京</B>"; //等同于$Cities[0] = "北京" $Cities[] = ...
- Ubuntu不会放弃32位应用程序
Ubuntu 开发人员澄清,人们以为 Ubuntu 将在 Ubuntu 19.10 和后续版本中放弃对运行 32 位应用程序的支持,但“根本不是这种情况”.那么这究竟是怎么一回事呢?前几天 Ubunt ...
- PyPI提供双因素身份验证(2FA),已提高下载安全性
前天,Python的核心开发团队宣布PyPI现在提供双因素身份验证(2FA),以提高Python包下载的安全性,从而降低未经授权的帐户访问的风险.该团队宣布将在Python Package Index ...
- 存储器HK1225-7EQ 使用说明书资料
一. 引脚排列 二. 读取模式 HK1225在WE(写使能)被禁止(high)且CE(片选)被选中(Low)且CE2(片选2)被选中(High)并且OE(读信号)被使能(Low)执行一次读循环.13个 ...
- hibernate字段值无法保存
通过hibernate对Blogstorage对象进行保存操作,filepath属性的值无论设置多少遍都保存不进去 后来发现是 Blogstorage.hbm.xml 里面根本没有配置filepath ...
- Django——HttpResponse()
HttpResponse(content, #返回给视图函数的内容 content_type=None,#返回给视图函数的类型 text/html文本.text/plain.css.js.xml.js ...
- 一百零八、SAP的OO-ALV之二,创建屏幕Screen
一.在资源管理器,右键->创建屏幕 二.输入4位数字 三.输入屏幕的描述 四.在逻辑流里面PBO用于显示屏幕,PAI用于用户交互. 五.在元素清单里面,在屏幕中的所有元素都是在元素清单中的