Find an Element by Class Name and Text in Selenium

In Selenium WebDriver with C#, if you want to find an element based on both its class name and the text it contains, you can use XPath or CSS selectors for more advanced matching. XPath allows you to traverse the HTML DOM and perform more complex queries to find elements.

Here’s an example that demonstrates how to find an element by its class name and text content:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace MySeleniumApp
    class Program
        static void Main(string[] args)
            // Initialize the Chrome Driver
            using (IWebDriver driver = new ChromeDriver())
                // Navigate to a website

                // Find element by its class name and text content using XPath
                IWebElement element = driver.FindElement(By.XPath("//*[contains(@class, 'element_class_name_here') and text()='element_text_here']"));

                // Perform actions on the element (e.g., click, send keys, etc.)
                // ... Your logic here ...

                // Close the driver