- Scaffold a
dotnet
console application:dotnet new console -n MultiBrowserTester
- Drop in the
MultipleWindowTest.csproj
file or... - Add Selenium 4:
dotnet add package Selenium.WebDriver
and... - Add WebDriverManager:
dotnet add package WebDriverManager
, then... - How to download the correct WebDriver for your Edge browser: https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=c-sharp#download-microsoft-edge-webdriver
- Decompress and place the
msedgedriver.exe
in the same folder as yourProgram.cs
class - Drop in the
Program.cs
file - Run the solution:
dotnet run
from within the project folder
Last active
May 3, 2023 15:29
-
-
Save HoraceBury/cc27aeb23d6561b228cbeb7f179d0ec9 to your computer and use it in GitHub Desktop.
Multiple Edge browser windows test in dotnet 6 C# with Selenium 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Selenium.WebDriver" Version="4.9.0" /> | |
<PackageReference Include="WebDriverManager" Version="2.16.2" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usings | |
using OpenQA.Selenium.Edge; | |
// generic driver options | |
List<string> Options = new List<string>() { | |
// "headless", // add this to run the browsers invisibly | |
"no-sandbox", | |
"incognito", | |
"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", | |
$"--window-size=1024,768", | |
}; | |
// edge options 1 | |
var options = new EdgeOptions(); | |
options.LeaveBrowserRunning = false; | |
options.AcceptInsecureCertificates = true; | |
options.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"; | |
// add options 1 arguments | |
Options.ForEach(arg => { if (!String.IsNullOrEmpty(arg)) options.AddArgument(arg); }); | |
// Important: Define profile folder for first browser | |
options.AddArgument($@"--user-data-dir={Directory.GetCurrentDirectory()}\prof-1"); | |
// edge options 2 | |
var options2 = new EdgeOptions(); | |
options2.LeaveBrowserRunning = false; | |
options2.AcceptInsecureCertificates = true; | |
options2.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"; | |
// add options 1 arguments | |
Options.ForEach(arg => { if (!String.IsNullOrEmpty(arg)) options2.AddArgument(arg); }); | |
// Important: Define profile folder for second browser | |
options2.AddArgument($@"--user-data-dir={Directory.GetCurrentDirectory()}\prof-2"); | |
// initialise driver service | |
using (var _service = OpenQA.Selenium.Edge.EdgeDriverService.CreateDefaultService(Directory.GetCurrentDirectory(), @"msedgedriver.exe")) | |
{ | |
_service.Start(); | |
// initialise first edge driver | |
using (var driver = new EdgeDriver(_service, options)) | |
{ | |
// define short default wait times | |
driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 2); | |
driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 0, 20); | |
driver.Manage().Timeouts().AsynchronousJavaScript = new TimeSpan(0, 0, 10); | |
// send first browser window to a page | |
driver.Navigate().GoToUrl("https://www.google.com/"); | |
// initialise second edge driver | |
using (var driver2 = new EdgeDriver(_service, options2)) | |
{ | |
// define short default wait times | |
driver2.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 2); | |
driver2.Manage().Timeouts().PageLoad = new TimeSpan(0, 0, 20); | |
driver2.Manage().Timeouts().AsynchronousJavaScript = new TimeSpan(0, 0, 10); | |
// send second browswer window to a page | |
driver2.Navigate().GoToUrl("https://www.youtube.com/"); | |
// wait so the user can see what's happening | |
Thread.Sleep(5000); | |
// stop browsers | |
driver.Quit(); | |
driver2.Quit(); | |
} | |
} | |
} | |
// using blocks will tidy up processes - check Task Manager for `Microsoft Edge WebDriver` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment