Find HTML Element by Id in Selenium

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
                driver.Navigate().GoToUrl("https://www.example.com");

                // Find element by its id
                IWebElement element = driver.FindElement(By.Id("element_id_here"));

                // Perform actions on the element (e.g., click, send keys, etc.)
                string elementText = element.Text;

                // Output the text of the element
                Console.WriteLine($"Element text is: {elementText}");

                // Close the driver
                driver.Quit();
            }
        }
    }
}

 

Running Selenium in a virtual desktop environment on Linux

Running Selenium tests in a virtual desktop environment on Linux can be particularly useful for isolating the browser and running tests as if they were in a GUI, without interfering with the actual display. A common tool for creating a virtual desktop on Linux is Xvfb (X Virtual FrameBuffer).

First, you need to install Xvfb. You can install it using your distribution’s package manager. For Ubuntu/Debian:

sudo apt-get update
sudo apt-get install -y xvfb

Start Xvfb on a particular display

Xvfb :99 -ac &

Export DISPLAY Environment Variable

You need to tell your terminal session to send any graphical output to the virtual frame buffer set up by Xvfb. You can do this by setting the DISPLAY environment variable.

export DISPLAY=:99

Run Your Selenium Test

Now you can run your Selenium test as you would normally, and it will send its output to the virtual frame buffer instead of trying to use a physical display.

Here is a sample C# code snippet that opens Google in Chrome. Make sure you have the Selenium WebDriver NuGet package and ChromeDriver installed.

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

namespace MySeleniumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize ChromeDriver
            using (IWebDriver driver = new ChromeDriver())
            {
                // Navigate to Google
                driver.Navigate().GoToUrl("https://www.google.com");
                
                // Do your test logic here
                
                // Close the driver
                driver.Quit();
            }
        }
    }
}

Stop Xvfb

After you’ve finished running your tests, you can stop the Xvfb process.

killall Xvfb

Configure Xvfb directly within your C# code

To configure a virtual desktop like Xvfb directly within your C# code, you can use the System.Diagnostics.Process class to start and stop the Xvfb process. This approach can be useful for programmatically managing the virtual display environment.

Here’s an example that demonstrates how to start Xvfb, set the DISPLAY environment variable, and then run a Selenium test—all within the C# code.

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

namespace MySeleniumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Start Xvfb on display 99
            Process xvfbProcess = new Process();
            xvfbProcess.StartInfo.FileName = "Xvfb";
            xvfbProcess.StartInfo.Arguments = ":99 -ac";
            xvfbProcess.StartInfo.RedirectStandardOutput = true;
            xvfbProcess.StartInfo.UseShellExecute = false;
            xvfbProcess.StartInfo.CreateNoWindow = true;
            xvfbProcess.Start();

            // Wait for Xvfb to initialize
            System.Threading.Thread.Sleep(2000);

            // Set the DISPLAY environment variable
            Environment.SetEnvironmentVariable("DISPLAY", ":99");

            // Initialize ChromeDriver
            using (IWebDriver driver = new ChromeDriver())
            {
                // Navigate to Google
                driver.Navigate().GoToUrl("https://www.google.com");

                // Do your test logic here

                // Close the driver
                driver.Quit();
            }

            // Stop Xvfb process
            xvfbProcess.Kill();
        }
    }
}

By using this approach, you can encapsulate the entire process of setting up a virtual display and running a Selenium test in your C# code, making it easier to integrate into your automated testing workflow.

Running Selenium without a GUI

Running Selenium without a GUI is known as running it in “headless” mode. In this mode, the browser doesn’t display a user interface, which is particularly useful for automated tests that run on servers or other environments where a graphical display is not available.

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

namespace MySeleniumApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize ChromeDriver options
            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--headless");  // Run headless
            
            // Initialize the Chrome Driver with options
            using (IWebDriver driver = new ChromeDriver(options))
            {
                // Navigate to Google
                driver.Navigate().GoToUrl("https://www.google.com");

                // Optionally, capture the page title to verify
                Console.WriteLine("Page title is: " + driver.Title);

                // Close the driver
                driver.Quit();
            }
        }
    }
}

Chrome will run in headless mode, performing all the actions but without displaying the browser GUI. You should see the page title printed in the console if everything works correctly.

The ability to run tests in headless mode can be particularly useful in an automated testing environment or a CI/CD pipeline.

Install Selenium WebDriver for .NET Core

Run the following command to install the Selenium WebDriver NuGet package:

dotnet add package Selenium.WebDriver

You’ll also need to download the WebDriver executable for the browser you intend to use. For example, if you are planning to use Chrome, you’ll need to download ChromeDriver.

You can download it from the official site and place the executable in a directory that is in your system’s PATH, or specify its location in your code.

Write Your Test Code

Now you can write your Selenium test code in the Program.cs file. Here’s a simple example to open Google’s homepage:

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 Google
                driver.Navigate().GoToUrl("https://www.google.com");

                // Close the driver
                driver.Close();
            }
        }
    }
}

 

Ignoring SSL certificate errors in C# RestSharp Library

//bypass ssl validation check by using RestClient object
var options = new RestClientOptions(baseurl) {
    RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
};
var restClient = new RestClient(options);

or in application level

//bypass ssl validation check globally for whole application.
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

 

Ignoring SSL certificate errors in C# HttpClient

var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback = 
    (httpRequestMessage, cert, cetChain, policyErrors) =>
{
    return true;
};

var client = new HttpClient(handler);

 

Checkpointing time series using Singular Spectrum Analysis (SSA) model

This code is using the CheckPoint method of the TimeSeriesPredictionEngine class in ML.NET. This method saves the state of the time series model to a file, so that it can be loaded and used for predictions later. For example, if you have a time series model that detects change points in data, you can use the CheckPoint method to save the model after training and then load it in another application to make predictions on new data.

SsaForecastingTransformer forecaster = forecastingPipeline.Fit(trainingData);

var forecastEngine = forecaster.CreateTimeSeriesEngine<ModelInput, ModelOutput>(mlContext);

// save model zip file
forecastEngine.CheckPoint(mlContext, ModelPath);

Load a List of Objects as Dataset in ML.NET

In ML.NET, you can load a list of objects as a dataset using the DataView API. ML.NET provides a flexible way to represent data as DataView, which can be consumed by machine learning algorithms. To do this, you’ll need to follow these steps:

  1. Define the class for your data objects: Create a class that represents the structure of your data. Each property of the class corresponds to a feature in your dataset.
  2. Create a list of data objects: Instantiate a list of objects with your data. Each object in the list represents one data point.
  3. Convert the list to a DataView: Use the MLContext class to create a DataView from the list of objects.

Here’s a step-by-step implementation:

Step 1: Define the class for your data objects

Assuming you have a class DataObject with properties Feature1, Feature2, and Label, it should look like this:

public class DataObject
{
    public float Feature1 { get; set; }
    public float Feature2 { get; set; }
    public float Label { get; set; }
}

Step 2: Create a list of data objects

Create a list of DataObject instances containing your data points:

var dataList = new List<DataObject>
{
    new DataObject { Feature1 = 1.2f, Feature2 = 5.4f, Label = 0.8f },
    new DataObject { Feature1 = 2.1f, Feature2 = 3.7f, Label = 0.5f },
    // Add more data points here
};

Step 3: Convert the list to a DataView

Use the MLContext class to create a DataView from the list of objects:

using System;
using System.Collections.Generic;
using Microsoft.ML;

// ...

var mlContext = new MLContext();

// Convert the list to a DataView
var dataView = mlContext.Data.LoadFromEnumerable(dataList);

Now you have the dataView, which you can use to train and evaluate your machine learning model in ML.NET. The DataView can be directly consumed by ML.NET’s algorithms or be pre-processed using data transformations.

Remember to replace DataObject with your actual class and modify the properties accordingly based on your dataset.

Load a Text File Dataset in ML.NET

Introduction

Machine learning has revolutionized the way we process and analyze data, making it easier to derive valuable insights and predictions. ML.NET, developed by Microsoft, is a powerful and user-friendly framework that allows developers to integrate machine learning into their .NET applications. One of the fundamental tasks in machine learning is loading datasets for model training or analysis. In this blog post, we’ll explore how to load a text file dataset using ML.NET and prepare it for further processing.

The Dataset

Let’s start with a simple dataset stored in a text file named data.txt. The dataset contains two columns: “City” and “Temperature”. Each row corresponds to a city’s name and its respective temperature. Here’s how the data.txt file looks:

City,Temperature 
Rasht,24 
Tehran,28 
Tabriz,8 
Ardabil,4

The Data Transfer Object (DTO)

In ML.NET, we need to create a Data Transfer Object (DTO) that represents the structure of the data we want to load. The DTO is essentially a C# class that matches the schema of our dataset. In our case, we’ll define a DataDto class to represent each row in the data.txt file. Here’s the DataDto.cs file:

public class DataDto
{
    [LoadColumn(0), ColumnName("City")] 
    public string City { get; set; }
    
    [LoadColumn(1), ColumnName("Temperature")]
    public float Temperature { get; set; }
}

The DataDto class has two properties, City and Temperature, which correspond to the columns in the dataset. The properties are decorated with attributes: LoadColumn and ColumnName. The LoadColumn attribute specifies the index of the column from which the property should load its data (0-based index), and the ColumnName attribute assigns the name for the corresponding column in the loaded data.

Loading the Dataset

With the DTO in place, we can now proceed to load the dataset using ML.NET. The entry point for ML.NET operations is the MLContext class. In our Program.cs, we’ll create an instance of MLContext, specify the path to the text file, and load the data into a DataView.

using System;
using Microsoft.ML;

public class Program
{
    static void Main()
    {
        // Create an MLContext
        var mlContext = new MLContext();
        
        // Specify the path to the text file dataset
        string dataPath = "data.txt";
        
        // Load the data from the text file into a DataView using the DataDto class as the schema
        var dataView = mlContext.Data.LoadFromTextFile<DataDto>(dataPath, separatorChar: ',', hasHeader: true);
        
        // Now you can use the dataView for further processing, like training a model, data analysis, etc.
        // ...
    }
}

The LoadFromTextFile method takes the path to the dataset file (dataPath) as well as the separator character (, in our case) and a boolean indicating whether the file has headers (hasHeader: true).

Conclusion

In this blog post, we’ve learned how to load a text file dataset in ML.NET using a Data Transfer Object (DTO) to define the structure of the data. By leveraging the LoadFromTextFile method, we can easily read the dataset into a DataView and utilize it for further processing, such as training a machine learning model or conducting data analysis. ML.NET simplifies the process of integrating machine learning capabilities into .NET applications, making it accessible to a broader range of developers and opening up new possibilities for data-driven solutions.

Install the ML.NET Command-Line Interface (CLI) tool

Windows

dotnet tool install --global mlnet-win-x64

Linux

dotnet tool install --global mlnet-linux-x64

Install a specific release version

If you’re trying to install a pre-release version or a specific version of the tool, you can specify the OS, processor architecture, and framework using the following format:

dotnet tool install -g mlnet-<OS>-<ARCH> --framework <FRAMEWORK>
dotnet tool install -g mlnet-linux-x64 --framework net7.0

Update the CLI package

dotnet tool list --global
dotnet tool update --global mlnet-linux-x64

References
https://learn.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/install-ml-net-cli