Codementor Events

Automate Web Browser in C# using (selenium) library

Published Jan 22, 2021

This article is all about of selenium using C# Programming language.
Before moving to next step.
you must need to know what selenium is and why we use it.


How to load browser in C# programming language
So, let start the with browser concept in c# language,
I will use coding snippets to understand the code in better way.
Once you have installed the selenium packages from NPM then put the library namespace into you code file.like below.

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

Note:
you must have to download the chrome web driver from the below link
https://chromedriver.chromium.org/downloads
one you have download the .exe file, then put it under the Debug folder.
then your browser will load otherwise it will throw exception.

After that i am writing a generic function for leading a browser.

  IWebDriver driver = null;
     private void LoadBrowser(string Link)
      {
              var DeviceDriver = ChromeDriverService.CreateDefaultService();
              DeviceDriver.HideCommandPromptWindow = true;
              ChromeOptions options = new ChromeOptions();
              options.AddArguments("--disable-infobars");
              driver = new ChromeDriver(DeviceDriver, options);
              driver.Manage().Window.Maximize();
              driver.Navigate().GoToUrl(Link);
              driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
      }

Let take first line of c# code.

var DeviceDriver = ChromeDriverService.CreateDefaultService();

This code will generate the default service of chrome driver.

DeviceDriver.HideCommandPromptWindow = true;

This command hide the chrome web driver command prompt while loading browser.

ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-infobars");
driver = new ChromeDriver(DeviceDriver, options);

This chunk of c# code add arguments to chrome running instance to hide the default bar.

driver.Manage().Window.Maximize();

This code maximize your web browser to full screen.

driver.Navigate().GoToUrl(Link);

and after that it will go the link given in the parameter of the function.

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

This is very important code, its mean that wait for 30 second if the browser did not find any elements in the DOM, after that it will go to the next statement of code.


I hope you understand it,
How web browser load in selenium this is very simple and to the point code for loading browsers.
if you have any question about it or need to learn you can conatct me any time.
I will reply you quries as answer you comments,
Thank you !
Hit Like šŸ˜ƒ

Discover and read more posts from Rizwan
get started
post commentsBe the first to share your opinion
francky donald
a year ago

Hi Rizwan, Iā€™m Francky and I please know if how to retrieve all the urls searched by the client in any browser on my computer.

Show more replies