首页 > 解决方案 > 如何使用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 获取所有出现的属性值。

标签: python-3.xseleniumxpathcss-selectorswebdriverwait

解决方案


这个答案是用 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"));
    }

推荐阅读