Reputation: 33
I have a the following structure within html:
<A>
<b>1</b>
<b>2</b>
<b>3</b>
</A>
How I can select the count of child nodes A using C# and webdriver?
Upvotes: 1
Views: 2319
Reputation: 963
This works...
Program.cs:
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
using (FirefoxDriver driver = new FirefoxDriver())
{
driver.Navigate().GoToUrl("file://" + AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "test.html");
//Use this line to capture all child "b" elements
var bElements = driver.FindElementByXPath("//A").FindElements(By.TagName("b"));
//Use the line below to capture all descendants
var bElements2 = driver.FindElementByXPath("//A").FindElements(By.XPath(".//*"));
//Use the line below to capture all immediate child elements
var bElements3 = driver.FindElementsByXPath("//A/*");
Console.WriteLine("Child elements1: " + bElements.Count);
Console.WriteLine("Child elements2: " + bElements2.Count);
Console.WriteLine("Child elements3: " + bElements3.Count);
driver.Close();
}
}
}
}
Test.html:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>test</title>
</head>
<body>
<A>
<b>1</b>
<b>2</b>
<b>3
<c>4</c>
</b>
<z>5</z>
</A>
</body>
</html>
Output:
Child elements1: 3
Child elements2: 5
Child elements3: 4
Upvotes: 2