You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用NUnit与Selenium统计DOM中ul下的li元素数量?

Counting <li> Elements with NUnit and Selenium

Got it, let's walk through exactly how to count the number of <li> elements under that specific <ul> (inside the div#selected) using NUnit and Selenium. Here's a practical, step-by-step solution with code examples:

Prerequisites

First, make sure your test project has the necessary NuGet packages installed:

  • Selenium.WebDriver (for core Selenium functionality)
  • NUnit and NUnit3TestAdapter (for running and managing NUnit tests)
  • Selenium.Support (optional, but adds handy utilities)

Full Test Example

Here's a complete NUnit test class that handles setup, counting, validation, and cleanup:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;

namespace YourTestProjectNamespace
{
    [TestFixture]
    public class ListItemCountTest
    {
        private IWebDriver _driver;

        [SetUp]
        public void TestSetup()
        {
            // Initialize ChromeDriver (ensure ChromeDriver is in your PATH or specify its file path)
            _driver = new ChromeDriver();
            _driver.Manage().Window.Maximize();
            
            // Navigate to the page containing your target DOM
            _driver.Navigate().GoToUrl("https://your-target-page-url.com");
        }

        [Test]
        public void VerifyLiElementCountUnderSelectedDiv()
        {
            // Option 1: First locate the parent <ul>, then fetch its child <li> elements
            IWebElement targetUl = _driver.FindElement(By.CssSelector("#selected ul"));
            IList<IWebElement> liElements = targetUl.FindElements(By.TagName("li"));
            
            // Option 2: Directly locate all <li> elements under the target <ul> (more concise)
            // IList<IWebElement> liElements = _driver.FindElements(By.CssSelector("#selected ul li"));

            // Get the total count of <li> elements
            int totalLiCount = liElements.Count;

            // Optional: Use NUnit assertion to validate the count (matches your example's 6 elements)
            Assert.That(totalLiCount, Is.EqualTo(6), "The number of <li> elements doesn't match the expected count");
        }

        [TearDown]
        public void TestTeardown()
        {
            // Clean up the WebDriver instance to avoid resource leaks
            _driver.Quit();
            _driver.Dispose();
        }
    }
}

Key Details

  • FindElements vs FindElement: We use FindElements because it returns an empty list if no matching elements are found (instead of throwing an exception), making it safe for reliable counting.
  • Locating Strategies:
    • CSS Selectors are clean and readable: #selected ul targets the <ul> inside the div with id selected; #selected ul li directly targets all child <li> elements of that <ul>.
    • If you prefer XPath, you can use //div[@id='selected']/ul for the parent <ul>, or //div[@id='selected']/ul/li for the <li> elements.
  • Assertion: The Assert.That line validates that the count matches your expected number (6 in the provided DOM example). This is optional but recommended to turn a simple count into a verifiable test.

内容的提问来源于stack exchange,提问作者Podpryadok Igor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:52:40