Selenium C# – Access Device Location

Selenium C# – Access Device Location
Introduction
Accessing a user's geolocation is super important when you're testing location-based features in web apps. With Selenium, you can easily simulate these browser behaviors by tweaking Chrome settings to allow location access. In this guide, I'll walk you through how to set up Chrome options for geolocation and automate geolocation access using Selenium in C#.
Code Implementation
Below is a C# Selenium script that configures Chrome options to allow geolocation access, navigates to a test page, and interacts with the page to trigger location access.
Example: Access Device Location with Selenium in C#.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System.Globalization;
namespace SACSSAuto.QA.SeleniumPractice
{
[TestClass]
public class Selenium_79
{
/****
* Sample Code Definition :- Selenium - Access Device Location
* Developed By :- Pankaj Kumar
* Date of Creation :- 31-Jan-2025
* Project :- SACSSAUTO - C# [Web / Mobile / API] *
*/
[TestMethod]
public void Selenium_Test()
{
Console.WriteLine("Selenium - Access Device Location");
// Initialize Chrome options
ChromeOptions options = new ChromeOptions();
Dictionary<string, object> prefs = new Dictionary<string, object>();
Dictionary<string, object> profile = new Dictionary<string, object>();
Dictionary<string, object> contentSettings = new Dictionary<string, object>();
// Set Chrome options for geolocation permissions
// 0 - Default, 1 - Allow, 2 - Block
contentSettings["geolocation"] = 1;
profile["managed_default_content_settings"] = contentSettings;
prefs["profile"] = profile;
options.AddUserProfilePreference("profile", profile);
// Initialize Chrome WebDriver with options
IWebDriver driver = new ChromeDriver(options);
// Maximize the browser window
driver.Manage().Window.Maximize();
// Open the URL
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/geolocation");
// Wait for 5 seconds
Thread.Sleep(5000);
// Click on the button to access geolocation
driver.FindElement(By.XPath("//*[@id='content']/div/button")).Click();
// Wait for 5 seconds
Thread.Sleep(5000);
// Quit the driver
driver.Quit();
}
}
}
Steps Explained
1. Configuring Geolocation Permissions
The script sets Chrome preferences to allow geolocation access by modifying the profile's content settings.
2. Launching and Navigating
The browser opens the specified geolocation test URL and waits briefly.
3. Triggering Geolocation Access
It clicks the button on the page that prompts for location access.
4. Cleanup
After interaction, the browser session is terminated.
Expected Outcome
Chrome launches with geolocation access enabled.
Navigates to the geolocation test site.
Clicks the button to fetch location.
Closes the browser session.
Conclusion
This script shows you how to automate and test geolocation features using Selenium in C#. By tweaking Chrome options, you can simulate real user actions, which is perfect for creating strong test scenarios.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
