Selenium + Chrome WebDriver With .Net Core on Ubuntu

Coding > .Net Core | By: Team Appsious | Wednesday, 04 December 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 browsers. This article explains how to use the 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 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 a chrome driver in the wwwroot directory. The 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 a deferent directory but the 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 the chrome driver folder using the below command. Note the www-data user group and user.
    It should be the 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

Selenium + Chrome WebDriver With .Net Core on Ubuntu

Related Articles