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();
            }
        }
    }
}

 

Disable wermgr.exe on Windows

Using Services

  1. Press Win + R to open the Run dialog.
  2. Type services.msc and press Enter.
  3. Scroll down and locate the Windows Error Reporting Service.
  4. Right-click on it and choose Properties.
  5. In the Startup type dropdown, select Disabled.
  6. Click Apply and then OK.

Using Registry Editor

  1. Press Win + R to open the Run dialog.
  2. Type regedit and press Enter.
  3. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting.
  4. Create a new DWORD value named Disabled and set its value to 1.

 

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);

 

Preferred Challenges for Certbot

The preferred challenges for Certbot are usually one of the following:

  1. HTTP-01 Challenge: This is the most common challenge type. Certbot will create a temporary file on your web server, and the Let’s Encrypt servers will try to access that file over HTTP. You’ll need to make sure that port 80 is open and that your web server is configured to serve files from the hidden .well-known directory.
  2. DNS-01 Challenge: This challenge requires you to add a specific DNS TXT record to your domain’s DNS settings. This is often used when you need to obtain a wildcard certificate or when the HTTP challenge is not suitable. It might require manual intervention if you don’t have a DNS provider with an API that Certbot can use.
  3. TLS-ALPN-01 Challenge: This challenge requires setting up a special TLS certificate on your server and is less commonly used. It’s generally more complex to set up compared to the HTTP-01 challenge.

The HTTP-01 challenge is often the easiest to use, especially for standard web server setups, while the DNS-01 challenge is necessary for more complex scenarios like wildcard certificates.

You can specify the challenge type when running Certbot with the --preferred-challenges option, followed by the challenge type, such as:

certbot --preferred-challenges http

or

certbot --preferred-challenges dns

Keep in mind that depending on your specific setup and requirements, you might need to choose a specific challenge type or follow additional steps to successfully obtain a certificate.

Certbot Standalone Mode

sudo certbot certonly --standalone --preferred-challenges http -d example.com

When you run this command, Certbot will start a temporary web server on port 80 (unless specified otherwise) and will respond to the HTTP-01 challenge from Let’s Encrypt. Once the challenge is successfully completed, Certbot will obtain the certificate and save it to a location on your system.

Note that since the command uses the --standalone option, you’ll need to make sure that port 80 is not in use by any other service at the time you run the command, and you’ll also need to manually configure your web server to use the obtained certificate once it’s issued.

Modifying the column names of a Pandas DataFrame

Rename Specific Columns

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob'],
    'Age': [25, 30]
})

# Rename the 'Name' column to 'Full Name' and 'Age' to 'Age in Years'
df.rename(columns={'Name': 'Full Name', 'Age': 'Age in Years'}, inplace=True)

print(df)

Modify All Headers at Once

# Create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob'],
    'Age': [25, 30]
})

# New column names
new_columns = ['Full Name', 'Age in Years']

# Assign the new column names to the DataFrame
df.columns = new_columns

print(df)

Apply a Function to Headers

# Create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob'],
    'Age': [25, 30]
})

# Make all column names uppercase
df.columns = df.columns.str.upper()

print(df)

 

Remove Column from DataFrame in Pandas

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
})

# Print the original DataFrame
print("Original DataFrame:")
print(df)

# Remove the 'Age' column
df = df.drop(columns=['Age'])

# Print the updated DataFrame
print("\nDataFrame After Removing 'Age' Column:")
print(df)

Remember to assign the result back to the DataFrame (or to a new variable) if you want to keep the change. If you just want to remove the column temporarily for a specific operation, you can use the inplace=True argument to modify the DataFrame in place:

df.drop(columns=['Age'], inplace=True)

You can remove a column from a DataFrame by its index

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
})

# Print the original DataFrame
print("Original DataFrame:")
print(df)

# Index of the column to be removed
column_index = 1

# Get the name of the column at the specified index
column_name = df.columns[column_index]

# Drop the column by its name
df.drop(columns=[column_name], inplace=True)

# Print the updated DataFrame
print("\nDataFrame After Removing Column at Index 1:")
print(df)

Remove multiple columns at once from a DataFrame in Pandas

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago'],
    'Country': ['USA', 'USA', 'USA']
})

# Print the original DataFrame
print("Original DataFrame:")
print(df)

# List of columns to be removed
columns_to_remove = ['Age', 'Country']

# Drop the specified columns
df.drop(columns=columns_to_remove, inplace=True)

# Print the updated DataFrame
print("\nDataFrame After Removing Columns:")
print(df)

If you want to remove multiple columns by their indices, you can use the following code:

# List of column indices to be removed
column_indices_to_remove = [1, 3]

# Get the names of the columns at the specified indices
columns_to_remove = df.columns[column_indices_to_remove]

# Drop the specified columns
df.drop(columns=columns_to_remove, inplace=True)