【问题标题】:How to extract the value of the attribute picture using Selenium Python如何使用Selenium Python提取属性图片的值
【发布时间】:2019-06-13 02:13:59
【问题描述】:

作为测试的一部分,我一直在寻找 XPath 代码来获取 HTML 元素的属性值。

<div class="gallery-list">
<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>
<div>

我需要通过 xpath sl-video-preview 获取属性值 有些可以帮助我们。 谢谢

【问题讨论】:

    标签: selenium xpath css-selectors webdriverwait xpath-1.0


    【解决方案1】:

    这是您可以使用的通用 xpath。

    //figure[@class='figure hd']/picture
    

    而且你必须获得 'sl-video-preview' 属性。

    Chrome 控制台输出:

    【讨论】:

      【解决方案2】:

      要提取 attribue sl-video-preview 的值,因为该元素是 Angular 元素,您必须为 visibility_of_element_located() 诱导 WebDriverWait,您可以使用以下任一Locator Strategies:

      • 使用CSS_SELECTOR

        print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.gallery-list > figure.figure.hd > a > picture.ng-isolate-scope.sl-safe"))).get_attribute("sl-video-preview"))
        
      • 使用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"))
        
      • 注意:您必须添加以下导入:

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

      【讨论】:

      • 完善您的示例,可以满足我的需要.. 非常感谢 /o/
      • 在某些情况下,属性图片在 div gallery-list 中显示了很多次。我需要获取所有图形并将其放入循环中以逐个获取
      • @dmrpy 你能就你的新要求提出一个新问题吗? StackOverflow 贡献者将很乐意为您提供帮助。
      • 在下面提出新问题。
      【解决方案3】:

      我有问题,为什么在某些情况下标签图出现多次而@DebanjanB 的这个例子只得到一条记录,那么我需要所有结果。

      <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"))
      

      如何获取所有记录?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-08
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多