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

如何使用Robot Framework+SeleniumLibrary实现浏览器地理位置覆盖

问题根因说明

你原有Python代码的问题在于单独实例化了新的Chrome驱动,和Robot Framework中SeleniumLibrary启动的浏览器实例不是同一个,因此修改的地理位置配置不会生效。以下是两种可直接落地的解决方案:


方案1:修正自定义Python库实现地理覆盖

第一步:修改geo.py代码

调整逻辑获取SeleniumLibrary当前运行的驱动实例,代码如下:

from robot.libraries.BuiltIn import BuiltIn

def set_geo_location(latitude=41.8781, longitude=-87.6298, accuracy=100):
    # 获取SeleniumLibrary实例
    selenium_lib = BuiltIn().get_library_instance('SeleniumLibrary')
    # 获取当前激活的浏览器驱动
    driver = selenium_lib.driver
    # 执行CDP命令覆盖地理位置
    coordinate_config = {
        "latitude": latitude,
        "longitude": longitude,
        "accuracy": accuracy
    }
    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", coordinate_config)

第二步:调整robot测试用例

默认Chrome会禁止页面获取地理位置,需要新增启动配置并调用自定义关键字,调整后的代码如下:

***Settings***
Library             SeleniumLibrary
Library             geo.py

Suite Teardown      End    
***Variables***
# 配置Chrome启动参数,允许地理位置请求
${chrome_options}    add_experimental_option=prefs={"profile.default_content_setting_values.geolocation": 1}

***Test Cases***
Test Location
    Should appear the overrided location


***Keywords***
End
    Close All Browsers

Should appear the overrided location
    # 带权限配置启动Chrome
    Open Browser                        https://browserleaks.com/geo       Chrome    options=${chrome_options}
    # 调用自定义关键字设置地理位置
    Set Geo Location
    Wait Until Page Contains            HTML5 Geolocation API
    Scroll Element Into View            xpath://*[@id="footer"]     
    # 验证是否为配置的芝加哥(美国)位置,可按需修改验证规则
    Page Should Contain Element     xpath://*[@alt="US"]    
    Sleep                               5
    Close Browser

方案2:无需自定义Python库,直接用原生关键字实现

如果使用SeleniumLibrary 4.0及以上版本,可以直接用内置的Execute Chrome Devtools Protocol关键字完成配置,不需要额外编写Python文件,robot代码如下:

***Settings***
Library             SeleniumLibrary

Suite Teardown      End    
***Variables***
${chrome_options}    add_experimental_option=prefs={"profile.default_content_setting_values.geolocation": 1}

***Test Cases***
Test Location
    Should appear the overrided location


***Keywords***
End
    Close All Browsers

Should appear the overrided location
    Open Browser                        https://browserleaks.com/geo       Chrome    options=${chrome_options}
    # 直接执行CDP命令设置地理位置
    Execute Chrome Devtools Protocol    Emulation.setGeolocationOverride    latitude=41.8781    longitude=-87.6298    accuracy=100
    Wait Until Page Contains            HTML5 Geolocation API
    Scroll Element Into View            xpath://*[@id="footer"]     
    Page Should Contain Element     xpath://*[@alt="US"]    
    Sleep                               5
    Close Browser

如果偏好使用Create Webdriver关键字实现,仅需要把Open Browser的逻辑替换为以下写法即可,效果完全一致:

Create Webdriver    Chrome    options=${chrome_options}
Go To    https://browserleaks.com/geo

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 18:54:00