python-3.x - 如何使用Selenium Python提取属性图片列表的值
问题描述
我在下面有这段代码
<div class="gallery-list">
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-
scope sl-safe">
</a>
</figure>
<figure class="figure hd" ng-class="profileGallery.css" profile-item-
remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-
scope sl-safe">
</a>
</figure>
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-
scope sl-safe">
</a>
</figure>
<div>
Xpath
print(WebDriverWait(driver,
20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='gallery-
list']/figure[@class='figure hd']/a/picture[@class='ng-isolate-scope sl-
safe']"))).get_attribute("sl-video-preview"))
使用此 xpath 代码可以获得属性图的一条记录,但这是动态的,有时我有 1、2、3、12 或 50。如何通过具有动态列表大小的 xpath sl-video-preview 获取所有出现的属性值。
解决方案
这个答案是用 Java 写的。
// Find All elements at once
list<WebElement> pictureURLs = driver.findElements(By.xpath("//div[@class='gallery-
list']//a/picture"));
//Loop through the element list & print the required attributes
for(WebElement pictureurl:pictureURLs)
{
System.out.println(pictureurl..get_attribute("sl-video-preview"));
}
推荐阅读
- go - golang如何解析没有年份的时间,但默认为当前年份
- vue.js - 如何在 vue_app 中插入summernote?
- linux - 如何使用 linux/ovs 网桥设置 PTP 主节点
- html - 背景图像在两侧拉伸,未正确覆盖容器
- linux - 如何安装 Qt WebGL 插件
- javascript - 开发人员如何在部分之间创建动画过渡?
- log4j - 质量保证 | 关闭到控制台的日志记录(例如 INFO cqqaui.WebDriverCommandLogger - 命令:)
- python - TypeError:Unicode 对象必须在检查之前进行编码
- aws-lambda - AWS Amplify 数据存储查询和按日期排序
- automapper-10 - AutoMapper 10.0 或 AutoMapper.Data 5.0 现在在 AddDataReaderMapping 调用上抛出 InvalidOperationException