我在html中具有以下结构:
<A>
<b>1</b>
<b>2</b>
<b>3</b>
</A>
如何使用C#和Webdriver选择子节点A的数量?
这有效...
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>
输出:
Child elements1: 3
Child elements2: 5
Child elements3: 4
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句