在无头Chrome中设置传感器(位置) [英] Setting sensors (location) in headless Chrome

查看:467
本文介绍了在无头Chrome中设置传感器(位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Chrome Headless设置自定义位置坐标?我在 Devtools协议 API.有解决方法吗?

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?

推荐答案

我用Google对其进行了搜索,并获得了许多方法.我一一尝试,几乎所有的都过时了.然后我找到一个解决方案,使用chrome devtools协议来实现.

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.

下面的小示例代码,它使用最常见的工具硒执行chrome devtools协议命令.

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

这篇关于在无头Chrome中设置传感器(位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆