OpenQA.Selenium.By.ClassName C# (CSharp) Method

ClassName() public static method

Gets a mechanism to find elements by their CSS class.
If an element has many classes then this will match against each of them. For example if the value is "one two onone", then the following values for the className parameter will match: "one" and "two".
public static ClassName ( string classNameToFind ) : By
classNameToFind string The CSS class to find.
return By
        public static By ClassName(string classNameToFind)
        {
            if (classNameToFind == null)
            {
                throw new ArgumentNullException("classNameToFind", "Cannot find elements when the class name expression is null.");
            }

            if (new Regex(".*\\s+.*").IsMatch(classNameToFind))
            {
                throw new IllegalLocatorException("Compound class names are not supported. Consider searching for one class name and filtering the results.");
            }

            By by = new By();
            by.findElementMethod =
                (ISearchContext context) => ((IFindsByClassName)context).FindElementByClassName(classNameToFind);
            by.findElementsMethod =
                (ISearchContext context) => ((IFindsByClassName)context).FindElementsByClassName(classNameToFind);

            by.description = "By.ClassName[Contains]: " + classNameToFind;
            return by;
        }

Usage Example

 /// <summary>
 /// Gets a mechanism to find elements by their CSS class.
 /// </summary>
 /// <param name="classNameToFind">The CSS class to find.</param>
 /// <returns>A <see cref="OpenQA.Selenium.By"/> object the driver can use to find the elements.</returns>
 public static new SeleniumBy ClassName(string classNameToFind) => SeleniumBy.ClassName(classNameToFind);
All Usage Examples Of OpenQA.Selenium.By::ClassName