question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get Child object of a given element

See original GitHub issue

Hi, When using the Get WebElements keyword am getting a list of elements that fits my locator. Each element contains a child object, how can I get the child one?

example:

<a class="yt-uix-sessionlink spf-link " data-sessionlink="itct=CGEQlDUYAyITCOeh4-OEvM8CFQKkHAodw6QFfSiOHjIGZy1oaWdoWg9GRXdoYXRfdG9fd2F0Y2g" href="/watch?v=5i-94at2SDA" aria-hidden="true">
<div class="yt-thumb video-thumb">
<span class="yt-thumb-simple">
<img height="110" width="196" data-ytimg="1" alt="" src="https://i.ytimg.com/vi/5i-94at2SDA/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=YqxUUk-Z6HgOYPKBVVuMA9o1iaU" onload=";__ytRIL(this)">
</span>
</div>
<span class="**video-time**" aria-hidden="true">**3:34**</span>
/a>

I want to get the “video-time” inside a random a> element, is it possible?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
praveendvdcommented, Jan 8, 2021

Yes, this gets asked now and then. Also it’s technically possible to implement this in many ways. But unfortunately no one has seen this important enough to implement. Currently my development time is used to help https://github.com/MarketSquare/robotframework-browser

There is direct message, you can use execute script to create a varaible.

so i created a keyword

  Validate child element exists

it takes two argument parent(Webelement) and child(Xpath) to search for child inside the parent.

  Validate child element exists    ${parent}    ./h3

Assuming parent xpath to as : //ul[@id="top_menu"] , the above code will search for equalent xpath //ul[@id="top_menu"]/./h3 note taht context is parent and not root

so this will be equalent to xpath

*** Test Cases ***
Google Search
    [Tags]    you    probably    do    not    have    this    many    tags    in    real    life
    Wait Until Element Is Visible  CSS=[id="introduction-container"]
    ${parent}=    Get webelement    xpath=//*[@id="introduction-container"]
	${result}=    Validate child element exists    ${parent}    ./h3   
    Should Be True     ${result}

	
*** Keywords ***
Validate child element exists 
    [Arguments]    ${parent}   ${child} 
    ${child}=    Execute Javascript    return window.document.evaluate( arguments[1], arguments[0], null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;     ARGUMENTS    ${parent}    ${child}
    [Return]    ${child}
0reactions
lukasgebhardcommented, Jul 20, 2022

For SeleniumLibrary versions 5.0 and above, you can use chained locators.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Child Elements of a Node in JavaScript
In this tutorial, you will learn how to get the first child element, last child element, and all elements of a specified element....
Read more >
Getting the child objects from an object and create new ones ...
Here is one way to build an array with the three properties : $children = []; foreach( $parent->getChildren() as $child ){ $children[] ...
Read more >
Finding child objects by type and property values
All children of a specific object can be obtained via object.children(). This article concerns itself with only finding objects by certain ...
Read more >
Element.children - Web APIs - MDN Web Docs
Element.children includes only element nodes. To get all child nodes, including non-element nodes like text and comment nodes, use Node.
Read more >
Scripting API: Transform.GetChild - Unity - Manual
In this case "Transform child out of bounds" error will be given. ... of the first child of the Game Object this script...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found