【问题标题】:How to extract the value of the attribute picture list using Selenium Python如何使用Selenium Python提取属性图片列表的值
【发布时间】:2019-07-15 21:08:06
【问题描述】:

下面有这段代码

<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 代码可以获得一个属性图记录,但这是动态的,有时我有一个、两个、三个、十二个或五十个。 如何通过具有动态列表大小的 xpath sl-video-preview 获取所有出现的属性值。

【问题讨论】:

    标签: python-3.x selenium xpath css-selectors webdriverwait


    【解决方案1】:

    看来你已经很接近了。对于&lt;figure&gt; 节点,您已经考虑了figurehd 这两个类,但它们都没有,您需要遍历它们。此外,您需要使用visibility_of_all_elements_located() 而不是visibility_of_element_located(),并且您可以使用以下任一解决方案:

    • 使用CSS_SELECTOR

      print([my_elem.get_attribute("sl-video-preview") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.gallery-list figure.figure picture.ng-isolate-scope.sl-safe")))])
      
    • 使用XPATH

      print([my_elem.get_attribute("sl-video-preview") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='gallery-list']//figure[contains(@class, 'figure')]//picture[@class='ng-isolate-scope sl-safe']")))])
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您需要修改您的函数以使用 find_elements_by_xpath 返回 WebElementsList,因为您当前的代码仅返回与提供的定位器匹配的单个(第一个)WebElement。

        建议的代码更改:

        pictures = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//picture")))
        for picture in pictures:
            print(picture.get_attribute('sl-video-preview'))
        

        以后您可以考虑重构您的代码以使用Page Object Model Design Pattern,这样您就可以从lazy initialization 方法中受益,而不必一直定义Waits

        【讨论】:

        • 嗨。此示例有时有效,但返回 Nome 值或返回 `AttributeError: NoneType object has no attribute find_elements`
        • 嗨,我发现了问题,不是在 xpath 中,我在登录页面后放了一个 time.sleep(5) 以解决组件中的超时问题。再次感谢您的帮助。
        猜你喜欢
        • 1970-01-01
        • 2016-08-10
        • 1970-01-01
        • 2017-06-02
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        • 2021-09-08
        相关资源
        最近更新 更多