Yu Zhang
Yu Zhang

Reputation: 2149

How to locate a list of webelement using Css selector in C#

I am trying to locate a list of "x-tree-node" webelement from the following HTML page.

<div class = "x-tree-root-node"> </div>
  <li class = "x-tree-node"> </li>
    <ul class = "x-tree-node-ct"> </ul>
      <li class = "x-tree-node"> </li> #Starts from here, those elements are the list of 'x-tree-node' I want to find.
      <li class = "x-tree-node"> </li> #I want this to be found in my list.
      <li class = "x-tree-node"> </li> #I want this to be found in my list.
      <li class = "x-tree-node"> </li> #I want this to be found in my list.

The code I am using is:

    public By TopLevelNodeLocator {
        get { return By.CssSelector(".x-tree-root-node.x-tree-node.x-tree-node-ct.x-tree-node");  }
    }

    public IList<IWebElement> AllTopLevelNode {
        get { return WebDriver.FindElements(TopLevelNodeLocator);  }
    }

But the IList I get returned an timeout exception, which means nothing has been found.

Can anyone help? Thanks

Upvotes: 0

Views: 887

Answers (1)

aholt
aholt

Reputation: 2971

By.CssSelector(".x-tree-node")

if you just want the elements that are children of the unordered list:

By.CssSelector(".x-tree-node-ct .x-tree-node")

or

By.CssSelector(".x-tree-node-ct > .x-tree-node")

or

By.CssSelector("ul .x-tree-node")

or

By.CssSelector("ul > .x-tree-node")

There are a bunch of ways you can do this.

Your property name of TopLevelNodeLocator makes it sound like you are trying to locate the root node, though, just fyi.

Upvotes: 1

Related Questions