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

如何使用Selenium向Chrome发送POST请求且不新开标签页

Chrome地址栏POST请求(无新标签页)实现方案

1. data URI请求构造

你需要把所有待传参数都作为隐藏输入项嵌入form,同时添加自动提交脚本,避免手动触发提交,构造完成的地址栏内容如下:

data:text/html,<form action="http://web-site.com/" method="post"><input type="hidden" name="id" value="150"><input type="hidden" name="first_name" value="Alex"><input type="hidden" name="last_name" value="li"><input type="hidden" name="phone" value="1234567890"><input type="hidden" name="email" value="test@gmail.com"><input type="hidden" name="id_2" value="6373"><input type="hidden" name="buy_type" value="1"><script>document.forms[0].submit()</script></form>

说明:所有参数通过type="hidden"的input标签传递,script标签会在页面加载完成后自动提交表单,全程不会新开标签页,提交后会直接加载目标接口的返回内容

2. Selenium代码修正

你原有代码缺少WebDriverWait导入,同时新版本Selenium已废弃executable_path写法,修正后代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait

# 替换为你的chromedriver实际路径
s = Service(r"C:\chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=s, options=options)
wait = WebDriverWait(driver, 5)

# 先打开目标站点获取必要的Cookie(避免跨域/校验问题)
driver.get('http://web-site.com/')

# 执行POST请求
post_data_uri = 'data:text/html,<form action="http://web-site.com/" method="post"><input type="hidden" name="id" value="150"><input type="hidden" name="first_name" value="Alex"><input type="hidden" name="last_name" value="li"><input type="hidden" name="phone" value="1234567890"><input type="hidden" name="email" value="test@gmail.com"><input type="hidden" name="id_2" value="6373"><input type="hidden" name="buy_type" value="1"><script>document.forms[0].submit()</script></form>'
driver.get(post_data_uri)

注意:如果目标站点有CSRF校验,你需要先从原站页面中提取CSRF token,作为额外参数加入form中再提交,否则会被拦截。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 15:24:01