1

I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my testing.

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

I need get value of attribue by xpath sl-video-preview Some can help us. thks

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
dmrpy
  • 59
  • 1
  • 8

3 Answers3

1

Here is the generic xpath that you can use.

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

And you have to get 'sl-video-preview' attribute.

Chrome Console Output:

enter image description here

supputuri
  • 13,644
  • 2
  • 21
  • 39
0

To extract the value of the attribue sl-video-preview, as the element is an Angular element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using 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"))
    
  • Using 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"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

i have problem, why in some cases the tag figure appear multiple times and this example by @DebanjanB get only one record, then I need all results.

<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 Example that works:

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

How do to get all records?

dmrpy
  • 59
  • 1
  • 8