Selenium FirefoxDriver启动报错:URL解析失败/端口无效求助
解决FirefoxDriver启动时的阿拉伯数字端口解析错误
问题根源
你遇到的两个报错核心原因是系统默认使用阿拉伯-Indic数字(如٤٠١٩٦)而非ASCII标准数字(0-9)生成端口号,导致Selenium和geckodriver无法识别端口参数,最终无法创建会话。
解决方案
1. 修改系统区域设置(彻底解决)
- 打开Windows设置,进入「时间和语言」→「语言和区域」
- 点击「其他日期、时间和区域设置」→「更改日期、时间或数字格式」
- 在「格式」选项卡中,选择使用ASCII数字的区域格式(如「英语(美国)」「中文(简体,中国)」)
- 切换到「管理」选项卡,点击「更改系统区域设置」,确认所选区域使用ASCII数字,重启电脑生效
2. 手动指定ASCII端口号(临时 workaround)
如果暂时不想修改系统设置,可在代码中强制指定标准数字端口:
package Com.google.SeleniumNew; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import io.github.bonigarcia.wdm.WebDriverManager; import java.util.Map; import java.util.List; public class ChromeDriverDemo { public static void main(String[] args) { WebDriverManager.firefoxdriver().setup(); // 强制指定ASCII格式的端口号 System.setProperty("webdriver.gecko.driver.port", "4444"); FirefoxOptions options = new FirefoxOptions(); options.setCapability("moz:firefoxOptions", Map.of("args", List.of("--port=4444"))); WebDriver driver = new FirefoxDriver(options); driver.get("http://www.google.com"); } }
3. 版本兼容性验证
- 确认geckodriver与Firefox版本匹配:Firefox 115+对应geckodriver 0.33.0及以上(WebDriverManager会自动匹配,无需手动下载)
- Selenium 4.7.1与geckodriver 0.33.0兼容,无需降级版本
内容的提问来源于stack exchange,提问作者Hussien Saed
相关产品推荐
相关产品推荐

