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

如何使用Selenium在.Net+NUnit环境下提取md-card中的指定字符串

Extract Success Message from md-card Using Selenium (.NET + NUnit)

Hey there! Let's walk through how to pull that "Email is verified successfully!" string from an md-card component using Selenium with .NET and NUnit. I’ll share practical, reliable approaches you can implement right away.

Basic Approach: Locate md-card and Extract Text

First, you need to target the md-card element, then grab its text content. Here are two common positioning strategies (pick the one that fits your page’s DOM structure):

1. CSS Selector

If the md-card is unique on the page, or you can narrow it down with a class/attribute, use this:

// Initialize explicit wait to handle page loading
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

// Locate the md-card and get its text
var successCard = wait.Until(d => d.FindElement(By.CssSelector("md-card")));
string successMessage = successCard.Text.Trim();

If there are multiple md-cards, add a specific class or attribute to make the selector unique (e.g., md-card.success-alert if it has a "success-alert" class).

2. XPath (Great for Text-Based Matching)

If you want to directly target the md-card containing your success message, use XPath’s contains() function:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var successCard = wait.Until(d => d.FindElement(By.XPath("//md-card[contains(text(), 'Email is verified successfully!')]")));
string successMessage = successCard.Text.Trim();

If the text lives inside a child element of md-card (like a <p> or <div>), adjust the XPath to target that child:

By.XPath("//md-card//*[contains(text(), 'Email is verified successfully!')]")

Full NUnit Test Example

Here’s a complete test method that includes waiting, extraction, and assertion (best practice for NUnit):

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;

[TestFixture]
public class EmailVerificationTests
{
    private IWebDriver driver;

    [SetUp]
    public void Setup()
    {
        // Initialize your driver here (e.g., ChromeDriver)
        driver = new OpenQA.Selenium.Chrome.ChromeDriver();
        driver.Navigate().GoToUrl("your-verification-page-url");
    }

    [Test]
    public void VerifySuccessMessageIsDisplayed()
    {
        // Wait for the success card to become visible
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
        var successMessageElement = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("md-card")));
        
        // Extract and clean the message text
        string extractedMessage = successMessageElement.Text.Trim();
        
        // Assert the message matches our expected text
        Assert.That(extractedMessage, Is.EqualTo("Email is verified successfully!"));
    }

    [TearDown]
    public void TearDown()
    {
        driver.Quit();
    }
}

Key Tips for Reliability

  • Always use explicit waits: Avoid Thread.Sleep()—WebDriverWait waits until the element is ready, making your tests more stable across different environments.
  • Make locators unique: If your page has multiple md-cards, use additional attributes (like id, class, or parent elements) to avoid selecting the wrong one. For example: By.CssSelector("div.verification-container md-card").
  • Handle hidden text (if needed): If the message is technically hidden (unlikely for a success alert), use successCard.GetAttribute("textContent") instead of Text to retrieve all text inside the element.
  • Inspect the DOM: Use your browser’s dev tools (F12) to check the exact structure of the md-card—this will help you write precise locators.

内容的提问来源于stack exchange,提问作者asha.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:05:47