Windows Server 2019下Selenium Server编码兼容配置求助
问题解决:Windows Server上Selenium Server兼容多种Content-Type编码的方法
问题现象
- 在Windows Server 2019上执行命令
java -jar selenium-server-4.9.0.jar standalone启动Selenium独立服务后,使用Python发送请求时,仅当请求头设置为{'Content-Type': 'application/json; charset=utf-8'}才能正常创建会话;仅设置{'Content-Type': 'application/json'}时请求失败。 - 但通过Docker运行
docker run -d -p 4444:4444 --name selenium selenium/standalone-chrome时,无论是否在请求头中指定编码,甚至使用ASCII编码都能正常工作。
环境信息
- Windows Server 2019
- Google Chrome Version 112.0.5615.138
- Selenium Server 4.9.0
- ChromeDriver 112.0.5615.49
解决方法
1. 启动Selenium Server时指定Java默认编码为UTF-8
Windows环境下Java默认字符编码与Docker中的Linux环境存在差异,导致Selenium Server对Content-Type的校验更严格。启动时添加-Dfile.encoding=UTF-8参数强制统一编码:
java -Dfile.encoding=UTF-8 -jar selenium-server-4.9.0.jar standalone
2. 升级Selenium Server到最新稳定版
Selenium 4.9.0属于较早版本,后续版本可能修复了Windows环境下的编码兼容性问题。下载并使用最新的Selenium Server稳定版重新启动服务,测试请求兼容性。
3. 通过配置文件调整HTTP服务器编码规则
创建selenium-config.toml配置文件,添加HTTP编码相关配置:
[server] port = 4444 [server.http] charset = "UTF-8"
启动服务时指定该配置文件:
java -jar selenium-server-4.9.0.jar standalone --config selenium-config.toml
内容的提问来源于stack exchange,提问作者aminmr1375
相关产品推荐
相关产品推荐

