Selenium + Chrome WebDriver With .Net Core on Ubuntu

By: Team Appsious | Sunday, September 22, 2019
Selenium + Chrome WebDriver With .Net Core on Ubuntu

Selenium is the most used web application testing framework. It is also a widely used framework for web data scrapping. Selenium has support for all the major web browser. This article explains how to use selenium framework with chromium driver in .net core hosted on Linux(Ubuntu).

Explaining how to create a .net core project is out of the scope of this article. Refer below links

Setup .net core project to use Selenium and Chrome WebDriver

  • Add Selenium+Chrome Web Driver NuGet package to the project.
    From visual studio package manager console run
    Install-Package Selenium.WebDriver

    If you are using vs code run below command from the console

    dotnet add package Selenium.WebDriver
  • Now create a new controller add below code to read webpage using Selenium and Chrome WebDriver
    public class HomeController : Controller
    {
       private readonly IHostingEnvironment _HostingEnvironment;
       public HomeController(IHostingEnvironment env)
       {
          _HostingEnvironment = env;
       }
       public IActionResult Index(string Url)
       {
         ChromeOptions option = new ChromeOptions();
         //Running chrome in headless mode
    option.AddArgument("--headless"); using (var driver = new ChromeDriver(_HostingEnvironment.WebRootPath, option)) {
    var title = driver.Title;
    } } }

    In the above the code I've used _HostingEnvironment.WebRootPath as chrome driver directory so our .net core code expects the driver to be in wwwroot directory. Let's install chrome driver in wwwroot directory. Chrome driver also needs the chrome binary to be present in Linux(Ubuntu) machine default location.

Installing Chrome WebDriver in Linux (Ubuntu)

  • First, install few dependencies for Google Chrome to run in Ubuntu, Open the terminal and run below command
    sudo apt-get install libxss1 libappindicator1 libindicator7 xvfb
  • XVFB is required to run the chrome in headless mode
  • Install Google Chrome for Ubuntu, Also note down chrome version while installing
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 
    sudo dpkg -i google-chrome*.deb
    sudo apt-get install –f
  • I'm going to install chrome driver in the wwwroot directory of my .net core web project in Nginx server,
    You can choose deferent directory but same should be specified in your c# code.
  • Changing the directory to wwwroot
    cd /var/www/html/myproject/wwwroot
  • Install Chrome driver, use this URL https://chromedriver.storage.googleapis.com/index.html to get chrome driver according to the chrome version number installed earlier.
    If there is no exact matching driver get the nearest one. Major version should be the same as chrome version installed, then run the below command
    wget -N http://chromedriver.storage.googleapis.com/<version>/chromedriver_linux64.zip
    unzip chromedriver_linux64.zip
  • Change the permission, give executable permission to chrome driver folder using below command. Note the www-data user group and user.
    It should be same as the one which is running your .net code in the webserver. If your project in a console project set the permission accordingly
    sudo chown -R www-data:www-data /var/www/html/myproject/wwwroot/chromedriver 
    sudo chmod +x chromedriver

Related Articles
Latest Articles
Free Creative Commons Zero (CC0) license Images websites for commercial use in 2019

Free Creative Commons Zero (CC0) license Images websites for commercial use in 2019

Websites with royalty-free creative commons zero (CC0) license Images for your blog, presentations or any commercial useThe pictures are free for personal and even for commercial use.You can modi...

Best Premium Stock Photography Websites to Buy/Sell Images in 2019

Best Premium Stock Photography Websites to Buy/Sell Images in 2019

These premium stock photography websites are useful for photographers to earn an extra bit of income. If you are a creative photographer, you might already have a collection of unused images stored in...

Top Coupons and Deals Websites in UK - 2019

Top Coupons and Deals Websites in UK - 2019

From gadgets to groceries, these websites make it easy to find the promos, coupons, deals, and discounts around you. There are many sites which provide deals and discounts coupons, here are the top si...

Top Coupons and Deals Websites in USA - 2019

Top Coupons and Deals Websites in USA - 2019

From gadgets to groceries, these websites make it easy to find the promos, coupons, deals, and discounts around you. There are many sites which provide deals and discounts coupons, here are the top si...

Best Online learning platform with video courses to upgrade your skill in 2019

Best Online learning platform with video courses to upgrade your skill in 2019

Today in the era of rapidly growing technology and ever-growing competition, it is becoming a mandate to upgrade your existing skills and also learn new and latest topics. People becoming expert in mu...

Best Google Chrome New Tab App/Extension

Best Google Chrome New Tab App/Extension

Although the latest version of the chrome has given the support for editable speed dial, where the user can customize and keep their own list of website.  Still, there are many nice apps/extensio...

Best Hashtag Generator for Instagram, Twitter, Pinterest, and Other Social Media

Best Hashtag Generator for Instagram, Twitter, Pinterest, and Other Social Media

Whether it is on Instagram or Twitter or Pinterest or any other social platform it is proven that post with hashtag reaches more audience. There is no doubt that the correct usage of a hasht...

Best Process Mining Software

Best Process Mining Software

The process mining software helps to identify the inefficiency in the process and to improve gaps. Process mining software analyzes application event logs to identify the bottleneck in the p...

Best Free Online Code Editor (Cloud IDE / Compiler) - 2019

Best Free Online Code Editor (Cloud IDE / Compiler) - 2019

What is the best Free Online Code Editor or Compiler? If you are a freelancer or you want to quickly try out some code and don't have access to the IDE or you are writing an article and want to includ...

Best free text editor and source code editor for Windows

Best free text editor and source code editor for Windows

Best text editor and source code editor for Windows....

Watch Blogs and Websites To Follow in 2019

Watch Blogs and Websites To Follow in 2019

If you are a fashion enthusiast, want to catch up with the latest news, reviews about the watch industries these are blogs and websites for you. This is a curated list of Watch Blogs and Web...

Websites for listening songs - Best online music streaming service 2019

Websites for listening songs - Best online music streaming service 2019

There are lots of online music streaming websites and tools which provides the streaming service for free or as a paid subscription. This list contains Websites for listening songs - Best online music...