You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Python修改地理位置时execute_cdp_cmd仅首次生效的问题

Selenium+Selenium Wire 修改地理位置不生效问题排查与解决

你使用Python的Selenium搭配Selenium Wire捕获HTTP请求时,尝试循环修改设备地理位置,但每次循环设置新坐标后,实际地理位置始终停留在首次设置的值,只有每次循环新建driver才生效,导致性能极差。代码如下:

from seleniumwire import webdriver

options = webdriver.EdgeOptions()
options.accept_insecure_certs = True
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Edge(seleniumwire_options={
                        'port': 12345, 'disable_encoding': True}, options=options)

# List of coordinates
lats = [44.55, 43.63, 52.25, 45.48, 47.35]
longs = [-77.33, -80.33, -81.62, -76.81, -84.62]

for i in range(len(lats)):
    coordinates = {
        "latitude": lats[i],
        "longitude": longs[i],
        "accuracy": i + 1
    }

    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", coordinates)
    driver.get(some_website)

    # I can see the coordinates passed to the `execute_cdp_cmd` changed every loop
    print(coordinates)

问题原因

Selenium Wire会在浏览器与目标网站之间创建代理,Emulation.setGeolocationOverride的CDP命令仅作用于当前浏览器上下文,但页面重复调用driver.get()时,可能触发代理或浏览器的缓存机制,保留初始地理位置信息;部分网站也会在首次加载时缓存地理位置数据,导致新设置的坐标无法生效。

解决方法

1. 清除旧设置后再更新新坐标

每次设置新坐标前,先执行清除地理位置覆盖的命令,确保旧设置被完全重置:

# 清除之前的地理位置覆盖
driver.execute_cdp_cmd("Emulation.clearGeolocationOverride", {})
# 设置新坐标
driver.execute_cdp_cmd("Emulation.setGeolocationOverride", coordinates)

2. 改用页面刷新替代重复调用driver.get()

首次加载页面使用driver.get(),后续循环改用driver.refresh(),避免重新发起完整请求触发缓存:

if i == 0:
    driver.get(some_website)
else:
    driver.refresh()

3. 禁用Selenium Wire缓存

初始化Selenium Wire时添加禁用缓存配置,避免代理缓存保留旧地理位置数据:

driver = webdriver.Edge(seleniumwire_options={
                        'port': 12345, 
                        'disable_encoding': True,
                        'disable_cache': True}, options=options)

调整后的完整代码

from seleniumwire import webdriver

options = webdriver.EdgeOptions()
options.accept_insecure_certs = True
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Edge(seleniumwire_options={
                        'port': 12345, 
                        'disable_encoding': True,
                        'disable_cache': True}, options=options)

# List of coordinates
lats = [44.55, 43.63, 52.25, 45.48, 47.35]
longs = [-77.33, -80.33, -81.62, -76.81, -84.62]

for i in range(len(lats)):
    coordinates = {
        "latitude": lats[i],
        "longitude": longs[i],
        "accuracy": i + 1
    }

    # 清除旧设置
    driver.execute_cdp_cmd("Emulation.clearGeolocationOverride", {})
    # 设置新坐标
    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", coordinates)
    
    # 首次加载/后续刷新
    if i == 0:
        driver.get(some_website)
    else:
        driver.refresh()
    
    print(coordinates)

内容的提问来源于stack exchange,提问作者legenddaniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 00:45:14