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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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
    

4 Comments

perfect your example, works fine for that i need.. thanks a lot /o/
in some cases, the atribute picture are displayed a lot of times in div gallery-list.. i need get all figure an put it inside a loop to get one by one
@dmrpy Can you raise a new question with your new requirement please? StackOverflow contributors will be happy to help you out.
put new question bellow.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.