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

求助:Selenium Python实现任意网站自动位置授权的方案

解决Selenium在电商/外卖网站自动授予位置权限的问题

针对Zomato、Swiggy这类网站,仅用Emulation.setGeolocationOverride和基础权限参数无法生效,核心原因是这类网站会做更严格的权限校验、弹窗拦截或依赖额外环境配置。以下是几个可行的解决方案:

方案1:组合Chrome权限参数+地理定位模拟

这类网站可能会检测Chrome的权限默认策略,需要补充更多参数强制授予地理位置权限,同时保留地理坐标模拟:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

def geoLocationTest():
    options = Options()
    # 强制默认允许地理位置权限
    options.add_argument("--permission-default=geolocation:allow")
    # 禁用权限提示管理器,避免弹窗干扰
    options.add_argument("--disable-features=PermissionPromptManager")
    # 启用地理定位功能
    options.add_argument("--geolocation")
    # 禁用隐私沙盒相关限制(部分网站依赖此检测)
    options.add_argument("--disable-features=PrivacySandboxSettings4")

    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    # 模拟目标坐标
    Map_coordinates = {
        "latitude": 28.697905669937008,
        "longitude": 77.17005421543875,
        "accuracy": 100
    }
    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", Map_coordinates)
    
    # 可选:模拟对应时区(部分网站会校验时区与坐标匹配)
    driver.execute_cdp_cmd("Emulation.setTimezoneOverride", {"timezoneId": "Asia/Kolkata"})
    
    driver.get("https://www.zomato.com/")
    time.sleep(10)
    raw_data = driver.find_element(By.XPATH,"//html")
    print(raw_data.text)

geoLocationTest()

方案2:通过CDP命令直接预授权网站权限

有些网站会在页面加载前检查权限状态,提前用Browser.setPermission命令授予目标网站地理位置权限,能跳过权限请求流程:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

def geoLocationTest():
    options = Options()
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    # 预授权Zomato的地理位置权限
    driver.execute_cdp_cmd("Browser.setPermission", {
        "origin": "https://www.zomato.com",
        "permission": {"name": "geolocation"},
        "setting": "granted"
    })
    
    # 模拟坐标
    Map_coordinates = {
        "latitude": 28.697905669937008,
        "longitude": 77.17005421543875,
        "accuracy": 100
    }
    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", Map_coordinates)
    
    driver.get("https://www.zomato.com/")
    time.sleep(10)
    raw_data = driver.find_element(By.XPATH,"//html")
    print(raw_data.text)

geoLocationTest()

方案3:自动化处理网站内置位置选择弹窗

部分网站即使权限已授予,仍会弹出手动位置选择框(比如Zomato的“使用当前位置”按钮),需要结合显式等待处理弹窗:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

def geoLocationTest():
    options = Options()
    options.add_argument("--permission-default=geolocation:allow")
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    Map_coordinates = {
        "latitude": 28.697905669937008,
        "longitude": 77.17005421543875,
        "accuracy": 100
    }
    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", Map_coordinates)
    
    driver.get("https://www.zomato.com/")
    
    # 等待并点击"使用当前位置"按钮(根据实际网站元素调整定位)
    wait = WebDriverWait(driver, 15)
    try:
        use_loc_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Use Current Location')]")))
        use_loc_btn.click()
    except:
        # 若未找到按钮,可能网站已自动加载位置,跳过
        pass
    
    time.sleep(10)
    raw_data = driver.find_element(By.XPATH,"//html")
    print(raw_data.text)

geoLocationTest()

注意事项

  • 不同网站的权限校验逻辑不同,可能需要组合上述方案使用;
  • 若网站使用了反爬机制,可能需要添加更多浏览器指纹模拟参数(如user-agent、窗口大小等);
  • 定期更新ChromeDriver版本,确保与Chrome浏览器版本兼容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 07:12:54