6

I'm using Selenium and trying to use CDP to mock Geolocation. But I'm having a problem that the ChromeDriver dont have anything like CreateDevToolsSession. This is the code that I've found in the Selenium Documentation:

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.DevTools.V87.Emulation;

namespace dotnet_test {
  class Program {
    public static void Main(string[] args) {
      GeoLocation().GetAwaiter().GetResult();
    }

    public static async Task GeoLocation() {
      ChromeDriver driver = new ChromeDriver();
      DevToolsSession devToolsSession = driver.CreateDevToolsSession();
      var geoLocationOverrideCommandSettings = new SetGeolocationOverrideCommandSettings();

      geoLocationOverrideCommandSettings.Latitude = 51.507351;
      geoLocationOverrideCommandSettings.Longitude = -0.127758;
      geoLocationOverrideCommandSettings.Accuracy = 1;

      await devToolsSession
        .GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V87.DevToolsSessionDomains>()
        .Emulation
        .SetGeolocationOverride(geoLocationOverrideCommandSettings);

        driver.Url = "<your site url>";
        }
    }
}

Thanks.

** UPDATE 1 ** This is the link for the documentation references. https://www.selenium.dev/documentation/webdriver/bidirectional/chrome_devtools/

2 Answers2

6

Selenium 4 Breaking Changes

CreateDevToolsSession() has been replaced with GetDevToolsSession().

A lot of examples you find online were written with the Beta version, like this one: https://dotjord.wordpress.com/2020/09/13/how-to-capture-network-activity-with-selenium-4-in-asp-net-core-3-1/ and this old code gets copied around https://stackoverflow.com/a/69478097/495455

Beta (old code):

IDevTools devTools = driver as IDevTools;
DevToolsSession session = devTools.CreateDevToolsSession();
session.Network.ResponseReceived += ResponseReceivedHandler;
session.Network.Enable(new EnableCommandSettings());
driver.Navigate().GoToUrl(url);
public void ResponseReceivedHandler(object sender, ResponseReceivedEventArgs e)
{
    System.Diagnostics.Debug.WriteLine($"Status: { e.Response.Status } : {e.Response.StatusText} | File: { e.Response.MimeType } | Url: { e.Response.Url }");
}

Alpha (working code):

using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V96.DevToolsSessionDomains;
var driver = new ChromeDriver();
var devTools = (IDevTools)driver;
IDevToolsSession session = devTools.GetDevToolsSession();
var domains = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
domains.Network.ResponseReceived += ResponseReceivedHandler;
await domains.Network.Enable(new OpenQA.Selenium.DevTools.V96.Network.EnableCommandSettings());
driver.Navigate().GoToUrl(url);

void ResponseReceivedHandler(object sender, ResponseReceivedEventArgs e)
{
    System.Diagnostics.Debug.WriteLine($"Status: { e.Response.Status } : {e.Response.StatusText} | File: { e.Response.MimeType } | Url: { e.Response.Url }");
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    this code does not work. problem with using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V96.DevToolsSessionDomains; – ozziem Aug 31 '22 at 09:31
2

In a addition to the nice answer from Jeremy Thompson, I wanted to share a new approach without being version and browser specific for Selenium 4+. (Works in Chrome and Edge with Selenium 4.8)

public void SetupNetworkLogging(IWebDriver driver)
{
    NetworkManager manager = new NetworkManager(driver);
    manager.NetworkResponseReceived += ResponseHandler;
    manager.StartMonitoring();
}

private void ResponseHandler(object sender, NetworkResponseReceivedEventArgs e)
{
    Console.WriteLine($"Http status: {e.ResponseStatusCode} : {e.ResponseBody} | Url: {e.ResponseUrl} ");
}

The official documentation for geolocation is now updated, for geolocation it seems to be nessessary, but if you don't need to specify a version use that:

IDevTools devTools = driver as IDevTools;
var session = devTools.GetDevToolsSession();

More documentation can be found here.

Schmebi
  • 355
  • 2
  • 16