Home > database >  Robotframework Selenium Library - extract clas sub-element
Robotframework Selenium Library - extract clas sub-element

Time:10-31

I need to extract an attribute "sub elemement" after initial selector.

I'm using this syntax as selector:

${first_video}=    Get WebElement    xpath:(//div[contains(@data-store, 'videoID')])[1]

This correctly give me this single element part:

<div  data-store="{"videoID":"1234","playerFormat":"inline","playerOrigin":"video_home","external_log_id":null,"external_log_type":null,"playerSuborigin":"entry_point",
"useOzLive":false,"playOnClick":true,
"videoScrollUseLowThrottleRate":true,"playInFullScreen":false,"type":"video","src":"https:\/\/video.test.com\/v\/a\/123_n.mp","width":320,"height":180,
"videoURL":"https:\/\/www.test.com\/test\/videos\/12345\/","disableLogging":false}" data-sigil="Video">
</div>

My goal is to get the VideoURL attribute.

I've tried with

${first_video_link}=    Call Method       ${first_video}    value_of_css_property    data-store.videoURL

but it gaves me empty output. Is there any specific syntax that I'm missing?

Many Thanks

CodePudding user response:

Get the value of the data-store atrribute:

${val}=    Get Element Attribute    ${your locator}

It appears to be a proper json, so convert it to a python dictionary and then just access its key:

${as json}=     Evaluate    json.loads("""${val}""")    json
Log To Console    The videoURL is ${as json['videoURL']} 
  • Related