Using IWebElement.FindElement to locate a descendant in C# Webdriver

3,361 views
Skip to first unread message

Erich

unread,
Jul 30, 2012, 6:15:01 PM7/30/12
to seleniu...@googlegroups.com
Hi!

I am trying to understand the scope when using IWebElement.FindElement(By by). My basic question is: does calling the method from an existing Web Element limit WebDriver's search to the descendants of that element, or will it still look over the entire page?

Example html:

<table id="importantTable">
  <tbody>
      <tr>
        <td class="strong">Item 1</td>
        <td class="weak">Item 2</td>
     </tr>
   </tbody>
</table>
<table id="NotImportantTable">
   <tbody>
     <tr>
       <td class="strong">Outlier 1</td>
       <td class="weak">Outlier 2</td>
     </td>
   </tbody>
</table>

Okay, so in my code, I want to retrieve the text from the first table's first column. So I was wondering if the following should work in C#:

IWebElement tableElement = driver.FindElement(By.CssSelector("table#importantTable"));
IWebElement cellElement = tableElement.FindElement(By.CssSelector("td.strong"));
string myText = cellElement.Text;

So I have been working under the impression that cellElement will uniquely locate the "Item 1" cell, because that cell is the only one available in the context of importantTable.  However, it appears (at least based on my locator strategies) that the code will fail, because it locates both elements.

I can use other strategies to locate that first element, using css or xpath, but I am curious about the effectiveness or purpose of using context elements to narrow the scope.

Thanks!

Peter Gale

unread,
Jul 30, 2012, 6:37:12 PM7/30/12
to Selenium Users
Does the same thing happen if you use Xpath locators to do the same searches?


Date: Mon, 30 Jul 2012 15:15:01 -0700
From: eri...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Using IWebElement.FindElement to locate a descendant in C# Webdriver
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/-kcx9NUULEwJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jim Evans

unread,
Jul 30, 2012, 6:38:52 PM7/30/12
to seleniu...@googlegroups.com
Your supposition is entirely correct[1]. If you're not seeing the behavior that the FindElement call is scoped, there's something else going on.

--Jim

[1] There's an exception in the case of XPath, where it's possible to scope the search to the entire document even on an element due to the very nature of XPath.

Erich

unread,
Jul 31, 2012, 11:14:36 PM7/31/12
to seleniu...@googlegroups.com
I have not noticed the same behavior with WebDriver, but that could simply be because I don't use XPath much. Additionally, since XPath has a relative vs. absolute notation, I figured it would probably work more naturally, since "./" tells the XPath engine to use the current node, whereas CSS doesn't really have a relative-absolute notation (of which I am aware).

-erich

On Monday, July 30, 2012 3:37:12 PM UTC-7, PeterJef...@Hotmail.co.uk wrote:
Does the same thing happen if you use Xpath locators to do the same searches?

Jim Evans

unread,
Aug 1, 2012, 6:18:26 AM8/1/12
to seleniu...@googlegroups.com
While you are correct that CSS selectors don't have a scoping mechanism as part of the syntax, there is an implementation detail that allows a scoping mechanism in practice. Selecting an element using CSS selectors is often done via the JavaScript querySelector() or querySelectorAll() functions. These are implemented in browsers for both document and element objects, giving the appropriate search scope. Under the hood, WebDriver uses exactly these functions for finding elements by CSS selectors, which gives the appropriate scope.

Mike Riley

unread,
Aug 8, 2012, 10:09:56 PM8/8/12
to seleniu...@googlegroups.com
You can always back up some levels and then go down from there using XPath.  For example if your element is an anchor tag inside a td, which would be inside a tr, you might nee to do something like "../../../tr/td/input" to get at an input tag in another row of the same table.

Mike
Reply all
Reply to author
Forward
0 new messages