Как извлечь значение атрибута картинки с помощью Selenium Python - PullRequest
1 голос
/ 13 июня 2019

Я искал код 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>

Мне нужно получить значение attribue с помощью xpath sl-video-preview Некоторые могут помочь нам.Thks

Ответы [ 3 ]

1 голос
/ 13 июня 2019

Вот общий xpath, который вы можете использовать.

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

И вы должны получить атрибут 'sl-video-preview'.

Вывод Chrome Console:

enter image description here

0 голосов
/ 07 июля 2019

У меня проблема, почему в некоторых случаях фигура тега появляется несколько раз, и этот пример @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"))

Как получить все записи?

0 голосов
/ 13 июня 2019

Чтобы извлечь значение атрибута sl-video-preview, так как элемент является элементом Angular , вы должны вызвать WebDriverWait для visibility_of_element_located() иВы можете использовать любую из следующих стратегий локатора :

  • Использование 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
    
...