在无头 Chrome 中设置传感器(位置)传感器、无头、位置、Chrome

由网友(浅时光﹏)分享简介:是否可以使用 Chrome Headless 设置自定义位置坐标?我在里面找不到Is it possible to set custom location coordinates with Chrome Headless? I can't find it in the Devtools protocolAPI. I...

是否可以使用 Chrome Headless 设置自定义位置坐标?我在里面找不到

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?

解决方案 开车告别手机 宝马iDrive新增实时路况功能

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.

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")

阅读全文

相关推荐

最新文章