Here is selenium c# script below to find duplicates in string and return the duplicates:
Example Code:
using System;
using System.Collections.Generic;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumSpecFlowDemo.Pages
{
class Program
{
[Test]
public void FindDuplicates()
{
IWebDriver driver = new ChromeDriver();
// Open application
driver.Navigate().GoToUrl("https://example.com");
// Get text from webpage element
string str = driver.FindElement(By.TagName("h1")).Text;
Dictionary<char, int> charCount = new Dictionary<char, int>();
foreach (char c in str.ToLower())
{
// Skip spaces
if (char.IsWhiteSpace(c))
continue;
// Count characters
if (charCount.ContainsKey(c))
charCount[c]++;
else
charCount[c] = 1;
}
Console.WriteLine("Duplicate characters in Web Element Text:");
// Print duplicate characters
foreach (var pair in charCount)
{
if (pair.Value > 1)
Console.WriteLine($"{pair.Key} : {pair.Value}");
}
// Close browser
driver.Quit();
}
}
}
Explanation:
irst, the required namespaces are imported:
SystemandSystem.Collections.Genericare used for basic C# functionality and dictionary collections.NUnit.Frameworkis used for the[Test]annotation to execute the method as a test case.OpenQA.SeleniumandOpenQA.Selenium.Chromeare used for Selenium WebDriver operations.
The class is created inside the namespace:
namespace SeleniumSpecFlowDemo.Pages
Inside the class, the FindDuplicates() method is marked with the [Test] attribute, which means NUnit will execute this method as a test case.
[Test]
public void FindDuplicates()
Inside the method, Selenium launches the Chrome browser using:
IWebDriver driver = new ChromeDriver();
Next, the browser navigates to the specified application URL:
driver.Navigate().GoToUrl("https://example.com");
After the webpage loads successfully, Selenium captures the text from the <h1> element using:
string str = driver.FindElement(By.TagName("h1")).Text;
The retrieved text is stored in the variable str.
To store the character count, a dictionary named charCount is created:
Dictionary<char, int> charCount = new Dictionary<char, int>();
In this dictionary:
- the key represents the character
- the value represents the number of occurrences of that character.
Next, a foreach loop iterates through each character in the string:
foreach (char c in str.ToLower())
The ToLower() method converts all characters into lowercase so that uppercase and lowercase letters are treated equally during comparison.
Inside the loop:
- First, the script checks whether the current character is a whitespace using:
char.IsWhiteSpace(c)
If the character is a space, the continue statement skips the current iteration and moves to the next character.
- If the character is not a space, the script checks whether the dictionary already contains the character:
charCount.ContainsKey(c)
- If the character already exists, its count is incremented:
charCount[c]++;
- Otherwise, the character is added to the dictionary with an initial count of
1:
charCount[c] = 1;
After counting all characters, another foreach loop is used to print only duplicate characters:
foreach (var pair in charCount)
The condition:
if (pair.Value > 1)
ensures that only characters appearing more than once are displayed.
Finally, the duplicate characters and their counts are printed in the console using:
Console.WriteLine($"{pair.Key} : {pair.Value}");
At the end of execution, the browser is closed using:
driver.Quit();