Setting sensors (location) in headless Chrome


Is it possible to set custom location coordinates with Chrome Headless? I can't find it in the Devtools protocol API. Is there a workaround available?


I googled it and got many methods. I try one by one, almost all of them turn out outdated. Then I find out a solution, use chrome devtools protocol to achieve that.

The small example code below, that it uses the most common tool selenium to execute chrome devtools protocol command.

<pre class="lang-py prettyprint-override">import time from selenium.webdriver import Chrome, ChromeOptions options = ChromeOptions() options.add_argument("--headless") driver = Chrome(options=options) driver.execute_cdp_cmd( "Browser.grantPermissions", { "origin": "https://www.openstreetmap.org/", "permissions": ["geolocation"] }, ) driver.execute_cdp_cmd( "Emulation.setGeolocationOverride", { "latitude": 35.689487, "longitude": 139.691706, "accuracy": 100, }, ) driver.get("https://www.openstreetmap.org/") driver.find_element_by_xpath("//span[@class='icon geolocate']").click() time.sleep(3) # wait for the page full loaded driver.get_screenshot_as_file("screenshot.png")





... then you'll need to contend with ensuring that the correct location sharing setting is set within the user profile (chrome://settings/content/location - which is difficult to access due to being displayed via shadow dom, so using a preconfigured user profile will likely be easier --user-data-dir).

Edit to add: The above does not seem to be effective when using --headless. To resolve this I used https://chromedevtools.github.io/devtools-protocol/tot/Page#method-addScriptToEvaluateOnNewDocument with the following snippet:

navigator.geolocation.getCurrentPosition = function(success, failure) { success({ coords: {latitude: <your_lat_float>, longitude: <your_lng_float>}, timestamp: Date.now(), }); }



  • PySide2 QMainWindow loaded from ui file not triggering window events
  • Setting sensors (location) in headless Chrome
  • Postgres - How to automatically call ST_SetSRID(ST_MakePoint(lng, lat), 4326) on inserts?
  • How to use nix's ioctl?
  • Where does the member variable inside a class allocated?
  • Eager Loading with Pagination
  • No perfect way to detect device orientation on iPad?
  • What is declare var in Node.js?
  • How to run python3.7 based flask web api on azure
  • How to apply CSS to document.write()?
  • Identify xml text elements with Schematron
  • NHibernate - Paging with ICriteria and optional ICriteria calls
  • Retrieving custom entities in CRM 4 C#
  • JQuery Cycle Plugin - Delay Interval Between Slides Question
  • How do I link when building with llvm libraries?
  • How to adapt DirectX-style world/view/projection matrices to OpenGL?
  • Get last 15 lines from a large file in SFTP with phpseclib [duplicate]
  • Do iTunesConnect external testers get updates for *every* uploaded ipa file after Beta Approval?
  • How to run Daphne Server (Django Channels) & workers in the background?
  • Slick: How can I combine a SQL LIKE statement with a SQL IN statement
  • iOS App crashed com.apple.root.background-qos
  • How to display youtube video on HTML page? [closed]
  • Draw 9 patch dotted /dashed line on Android
  • How to create OLE Automation to be used with ClearExplorer
  • Signed Java web start application with Glassfish 4.1 and Java7
  • Unsupported ciphersuite TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  • Showing image on a acro text field position
  • UIScrollView does not restore properly
  • Exclude Classes from Being Included in Shaded Jar
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Swift manually rotate view controller
  • readmore button or show/hide for php
  • select an input by value?
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Unable to create Access token grant type in wso2 API manager store to test API
  • Can a PHP script be scheduled to run at a specific time or after a specific amount of time has expir
  • How to encrypt Connectionstring written in web.config from codebehind?
  • How to call different template for different category archive page in woocommerce
  • Simple stitching in c++ using opencv