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.

Find parent element

See original GitHub issue

How do I get the parent element (or ancestor / ancestors ) of an element? The next element would also be usefull. I tried the following: element.GetAttribute("Ancestors") FindElement(By.XPath("./.."), element)

Both queries return null.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
huster-songtaocommented, Dec 8, 2021

@liljohnak many thanks

I search the parent node successfully using the session.

public static WindowsElement? GetParent(this WindowsDriver<WindowsElement> session, string xpath)
        {
            try
            {
                return session.FindElementByXPath($"{xpath.TrimEnd('/')}/parent::*");
            }
            catch (Exception)
            {
                return null;
            }
        }

The code above work well.

1reaction
trashbatcommented, Oct 7, 2020

Another workaround, using the element ID to build an XPath expression for the parent:

# Get the immediate parent:
xpath = f"//*[@RuntimeId='{element.id}']/parent::*"

# Get the first ancestor of type "Pane" named "foo":
xpath = f"//*[@RuntimeId='{element.id}']/ancestor::Pane[@Name='foo']"
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML DOM parentElement Property
The parentElement property returns the parent element of the specified element. ... In most cases, it does not matter which property you use,...
Read more >
JavaScript Get the Parent Element parentNode
JavaScript parentNode example · First, select the element with the .note class by using the querySelector() method. · Second, find the parent node...
Read more >
Node: parentElement property - Web APIs | MDN
The read-only parentElement property of Node interface returns the DOM node's parent Element , or null if the node either has no parent, ......
Read more >
Element: closest() method - Web APIs - MDN Web Docs - Mozilla
The closest() method of the Element interface traverses the element and its parents (heading toward the document root) until it finds a node...
Read more >
Get the Parent of an Element - JavaScript Tutorial
First, select the element with the CSS selector #main · Then, use the parentNode property to get the parent element of the selected...
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