Skip to content

Instantly share code, notes, and snippets.

@HoraceBury
Last active May 15, 2023 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HoraceBury/fa16c138a85ede55094a7cf5dcc1d5d9 to your computer and use it in GitHub Desktop.
Save HoraceBury/fa16c138a85ede55094a7cf5dcc1d5d9 to your computer and use it in GitHub Desktop.

Launch parallel threaded Edge browser windows with Selenium 4

Setup steps

  1. Scaffold a dotnet console application: dotnet new console -n ThreadedSelenium
  2. Drop in gist files
  3. Extract msedgedriver.exe to the project folder
  4. Run the solution with dotnet run

Notes

internal class Program
{
private static string[] urls = new string[] { "https://www.microsoft.com/", "https://www.youtube.com/", "https://www.airproducts.com/", "https://www.twitter.com/" };
private static void Main(string[] args)
{
PrepareTempFolders();
List<Runner> runners = new List<Runner>();
Console.WriteLine("INSTANTIATE...");
for (var index=0; index < urls.Length; index++)
runners.Add(new Runner(index: index, headless: false, urls[index]));
Console.WriteLine("START...");
foreach (var runner in runners)
runner.Start();
Console.WriteLine("JOIN...");
foreach (var runner in runners)
runner.Join();
Console.WriteLine("QUIT...");
foreach (var runner in runners)
runner.Quit();
}
public static void PrepareTempFolders()
{
var tmpPath = $@"{Directory.GetCurrentDirectory()}\tmp";
if (Directory.Exists(tmpPath))
Directory.Delete(tmpPath, true);
Directory.CreateDirectory(tmpPath);
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
internal class Runner
{
private int index;
private bool headless;
private string url;
private IWebDriver driver;
private Thread thread;
public Runner(int index, bool headless, string url)
{
this.index = index;
this.url = url;
driver = CreateDriver(index, headless);
thread = new Thread(() => Navigate(driver, url, index));
}
public void Start()
{
Console.WriteLine($"DRIVER {index} START");
thread.Start();
Console.WriteLine($"DRIVER {index} STARTED");
}
public void Join()
{
Console.WriteLine($"DRIVER {index} JOIN");
thread.Join();
Console.WriteLine($"DRIVER {index} JOINED");
}
public void Quit()
{
Console.WriteLine($"DRIVER {index} QUIT");
driver.Quit();
driver.Dispose();
Console.WriteLine($"DRIVER {index} QUITTED");
}
public static EdgeDriver CreateDriver(int index, bool headless = false)
{
var service = EdgeDriverService.CreateDefaultService(@".", @"msedgedriver.exe");
service.Start();
Console.WriteLine($"service.Port: {service.Port}");
var options = new EdgeOptions();
if (headless)
options.AddArgument("headless");
options.AddArguments(
"--disable-extensions",
"--window-size=1920,1080",
"inprivate",
"no-sandbox",
"inprivate",
"disable-gpu",
"ignore-certificate-errors",
"no-default-browser-check",
"disable-web-security",
"allow-insecure-localhost",
"allow-running-insecure-content",
"acceptInsecureCerts=true",
"proxy-server='direct://'",
"proxy-bypass-list=*",
"disable-extensions",
"disable-infobars"
);
options.AddArgument($"remote-debugging-port={9222+index}");
options.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
options.LeaveBrowserRunning = false;
options.AcceptInsecureCertificates = true;
options.AddArgument($@"--user-data-dir={Directory.GetCurrentDirectory()}\tmp\prof-{index}");
return new EdgeDriver(service, options);
}
public static void Navigate(IWebDriver driver, string url, int index)
{
driver.Navigate().GoToUrl(url);
driver.Manage().Window.Maximize();
Thread.Sleep(1000);
((EdgeDriver)driver).GetScreenshot().SaveAsFile($@"tmp\{index}.png");
Thread.Sleep(1000);
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Selenium.WebDriver" Version="4.9.1" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment