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

Docker中Selenium无法访问本地网络的问题求助

问题:Docker中Selenium无法访问本地网络站点

在Docker中运行Selenium时无法连接本地网络内的网站,但本地PC直接运行Selenium可以正常访问。现有一台DNS服务器位于192.168.1.132,希望通过Windows 11上的Docker Desktop(v24.0.6)爬取192.168.1.1的站点,但每次执行driver.Navigate()时C#代码都会抛出超时异常:

The HTTP request to the remote WebDriver server for URL http://localhost:4444/wd/hub/session/7ef05b4b64ea31e8d0e377c1cb833036/url timed out after 60 seconds.

代码示例

ChromeOptions options = new();
options.AddArgument("--ignore-ssl-errors=yes");
options.AddArgument("--ignore-certificate-errors");

IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);
try
{
    driver.Navigate().GoToUrl(configuration.IpToSearch); //IpToSearch = http://192.168.1.1 (无法访问) http://google.com (正常访问)
    var login = new WebDriverWait(driver, new TimeSpan(0, 0, 30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.ClassName("password-text")));
    login.SendKeys(configuration.Password);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    driver.Quit();
}

容器启动命令

docker run -d -p 4444:4444 --dns=192.168.1.132 -v /dev/shm:/dev/shm selenium/standalone-chrome

容器日志

2023-11-13 07:53:30 06:53:30.673 INFO [LocalNode.newSession] - Session created by the Node. Id: 7503932bc446870745db8cf625604178, Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 119.0.6045.123, chrome: {chromedriverVersion: 119.0.6045.105 (38c72552c5e..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:46267}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:bidiEnabled: false, se:cdp: ws://172.17.0.2:4444/sessio..., se:cdpVersion: 119.0.6045.123, se:vnc: ws://172.17.0.2:4444/sessio..., se:vncEnabled: true, se:vncLocalAddress: ws://172.17.0.2:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
2023-11-13 07:53:30 06:53:30.691 INFO [LocalDistributor.newSession] - Session created by the Distributor. Id: 7503932bc446870745db8cf625604178 
2023-11-13 07:53:30  Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 119.0.6045.123, chrome: {chromedriverVersion: 119.0.6045.105 (38c72552c5e..., userDataDir: /tmp/.org.chromium.Chromium...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:46267}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:bidiEnabled: false, se:cdp: ws://172.17.0.2:4444/sessio..., se:cdpVersion: 119.0.6045.123, se:vnc: ws://172.17.0.2:4444/sessio..., se:vncEnabled: true, se:vncLocalAddress: ws://172.17.0.2:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
2023-11-13 07:58:33 06:58:33.421 WARN [SpanWrappedHttpHandler.execute] - Unable to execute request: java.util.concurrent.TimeoutException
2023-11-13 07:58:33 Build info: version: '4.15.0', revision: '1d14b5521b'
2023-11-13 07:58:33 System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.90.1-microsoft-standard-WSL2', java.version: '11.0.20.1'
2023-11-13 07:58:33 Driver info: driver.version: unknown
2023-11-13 07:58:33 org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
2023-11-13 07:58:33 Build info: version: '4.15.0', revision: '1d14b5521b'
2023-11-13 07:58:33 System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.90.1-microsoft-standard-WSL2', java.version: '11.0.20.1'
2023-11-13 07:58:33 Driver info: driver.version: unknown
2023-11-13 07:58:33     at org.openqa.selenium.remote.http.jdk.JdkHttpClient.execute0(JdkHttpClient.java:410)

解决方案

1. 切换Docker网络模式为主机网络

Windows上Docker Desktop默认使用WSL2后端,容器处于独立虚拟网络,无法直接访问局域网设备。使用--network host参数让容器共享主机网络栈:

docker run -d --network host --dns=192.168.1.132 -v /dev/shm:/dev/shm selenium/standalone-chrome

注意:此模式下无需再映射端口-p 4444:4444,容器直接使用主机4444端口。

2. 用主机局域网IP替换localhost

若不想修改网络模式,可在C#代码中将localhost替换为主机的局域网IP(如192.168.1.x):

IWebDriver driver = new RemoteWebDriver(new Uri("http://192.168.1.xxx:4444/wd/hub"), options);

同时可进入容器验证局域网设备可达性:

docker exec -it <容器ID> ping 192.168.1.1

若ping不通,可检查WSL2网络配置,或切换到Docker Desktop的Windows容器模式尝试。

3. 验证DNS配置有效性

进入容器检查DNS是否生效:

docker exec -it <容器ID> cat /etc/resolv.conf
docker exec -it <容器ID> nslookup 192.168.1.1

若DNS解析异常,需确认DNS服务器192.168.1.132的可达性,或添加备用DNS地址。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 08:45:11