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

Selenium WebDriver连接Kameleo浏览器获取Chrome Session ID与端口问题

问题解决方法

首先明确:Kameleo Local API的/profiles/{guid}/start接口本身就会返回包含端口、WebSocket端点(内含Session ID)的结构体,对应的返回字段属于ProfileResponse结构体的内置属性,未单独在接口参数说明里高亮标注,所以容易被忽略。你之前没有接收start_profile方法的返回值所以无法获取对应参数。

修正后可直接获取参数的代码

from kameleo.local_api_client.kameleo_local_api_client import KameleoLocalApiClient
from kameleo.local_api_client.builder_for_create_profile import BuilderForCreateProfile

client = KameleoLocalApiClient()
base_profiles = client.search_base_profiles(
    device_type='desktop',
    browser_product='chrome'
)

# 创建带默认指纹防护配置的新配置文件
create_profile_request = BuilderForCreateProfile \
    .for_base_profile(base_profiles[0].id) \
    .set_recommended_defaults() \
    .build()
profile = client.create_profile(body=create_profile_request)

# 启动浏览器并接收启动返回结果
start_result = client.start_profile(profile.id)

# 直接从返回结果获取需要的参数
browser_port = start_result.port
ws_endpoint = start_result.ws_endpoint
# 从WebSocket端点提取Session ID,格式为 ws://localhost:端口/devtools/browser/[Session ID]
session_id = ws_endpoint.split('/')[-1]

print(f"浏览器端口:{browser_port}")
print(f"Session ID:{session_id}")

从状态接口获取参数的方法

如果你需要在浏览器运行过程中主动查询参数,可以调用get_profile接口获取:

# 运行过程中任意时刻查询配置文件状态
running_profile = client.get_profile(profile.id)
browser_port = running_profile.port
session_id = running_profile.ws_endpoint.split('/')[-1]

常见对接使用示例

如果要对接Selenium操控启动的Chrome,直接用获取到的端口构造远程控制地址即可:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("kameleo:profileId", profile.id)
driver = webdriver.Remote(
    command_executor=f'http://localhost:{browser_port}',
    options=options
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 18:36:05