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


Tags: c#,css,selenium

Problem :

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



Solution :

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.


    CSS Howto..

    How to align lists and text side by side with div tags?

    How can I set a CSS property on the html element with jQuery?

    How can I keep percentage-based elements from overlapping?

    How to add this background-image effect to a div?

    How to write a fallback for css files not loaded, loading too long and blocked? [duplicate]

    how to prevent transformation of text inherited from a skewed parent

    How to increase rotate value

    How to create a vertical line with a text in the middle

    How to identify unused css definitions [closed]

    How to add html classes to a Django template 'for-loop' dynamically?

    how to open a menu with a button?in html

    How to use scaleable pixels in css

    how to align div horizontally using CSS?

    How to use jquery to cycle elements bg color on click, and reset upon another element's click

    How to make a text or html tag look like a button

    How to set jquery ui icon as background image per css?

    How to add space between border line and menu item

    How to simulate \hfill with HTML and CSS?

    How to make line-through wider/bigger than text/element using CSS

    CSS - How do I get my list element to fill 100% of the block

    how to solve in conflict css?

    How can I float a bunch of divs and still get overflow?

    how to set html inline css from django

    How to handle long string where characters stick together

    How can I center Twitter-Bootstrap 3 navbar buttons?

    how to DRY up (remove redundancy) from this css class selector declaration?

    How to have a multiline CSS horizontal scroll?

    How to fix this arrow shape created using CSS in IE9

    CSS dropdown menu not showing the full texts of submenu items

    How To Adjust CSS List On Hover Image