Browser automation with Selenium

Selenium is a browser automation framework. This means that it can control the browser, in a manner similar to what you would do youself. For instance, it could be something like this:

  • Open Google Chrome
  • Navigate to Google.com
  • Type “Funny cat videos” into the search bar
  • Click the “Search” button
  • Open the first site in the search result.

Now besides the fact that it’s quite cool, why would you do something like this? Personally I use it for two things:

  1. To automate tests which would normally be performed by a human being.
  2. To automate monotonous tasks, freeing your employees (or yourself) to focus on the tasks that actually requires some thought work.

Whether you want to do one or another (or maybe something completely different), I will try to give a quick introduction to, how to get started.

Install the required packages

Selenium makes use of two different components: A library for your code (in this example a NuGet package) and a browser driver, which is an executable targeted on a specific browser (we will use the one for Google Chrome, but anything is fine).

The NuGet package can be installed with the following command (if you are using Visual Studio, you can also find it with the package manager):

dotnet add package Selenium.WebDriver

The browser driver are maintained by the respective project teams and not the Selenium team. However you can find a list of supported browsers on the Selenium website. For this guide, we will get the Chrome driver and place it in the same folder as our csproj file.

It is possible to place the driver in a different folder than the root. In that case you just have to specify the location when configuring the web driver.

Now all that is left, is the step that I always forget. To set the build action for chrome driver.exe to Copy always. This can be done by adding a few lines to your <ItemGroup> tag in the .csproj file, so it looks like this:

<ItemGroup>
  <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
  <Content Include="chromedriver.exe">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </Content> 
</ItemGroup>

Now with that in place, you can finally start writing some actual code.

Writing automation code

Now I have a confession. The following code is almost a direct copy of Selenium’s Getting Started article. But it is actually a quite good article, so no reason for any major changes.

static void Main()
{
    using (IWebDriver driver = new ChromeDriver())
    {
        // Create a helper which can wait for a page to load. 
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));

        // Navigate to Google.com
        driver.Navigate().GoToUrl("https://www.google.com/ncr");

        // Insert "Funny cat videos" in the search bar and press enter
        driver.FindElement(By.Name("q")).SendKeys("Funny cat videos" + Keys.Enter);

        // Select the first result and print the text with the first h3 element to the console
        IWebElement firstResult = wait.Until(drv => drv.FindElement(By.CssSelector("h3")));
        Console.WriteLine(firstResult.GetAttribute("textContent"));

        // Close the browser and the driver
        driver.Quit();
    }
}

The code does exactly what I described in the beginning of this post. It goes to Google.com and search for funny cat videos. Obviously this isn’t very useful, but there are plenty of good use for browser automation. Let me know if you are interested and I will write a couple of more detailed posts, on how to generate value with Selenium.

Published by Jakob Busk Sørensen

Software developer, currently focusing on process automation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: