Seleniumwire Remote替换Selenium Remote触发SessionNotCreatedException求助
super(Scraping, self).__init__(command_executor=command_executor, options=self.chrome_options, seleniumwire_options=self.seleniumwire_options)
...
触发报错:
selenium.common.exceptions.SessionNotCreatedException: Message: Could not start a new session. New session request timed out
Host info: host: 'mine', ip: '192.168.21.3'
Build info: version: '4.5.0', revision: 'fe167b119a'
System info: os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_341'
Driver info: driver.version: unknown
Stacktrace:
at org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue.addToQueue (LocalNewSessionQueue.java:196)
at org.openqa.selenium.grid.sessionqueue.NewSessionQueue.lambda$new$0 (NewSessionQueue.java:69)
at org.openqa.selenium.remote.http.Route$TemplatizedRoute.handle (Route.java:192)
at org.openqa.selenium.remote.http.Route.execute (Route.java:68)
at org.openqa.selenium.remote.http.Route$CombinedRoute.handle (Route.java:336)
at org.openqa.selenium.remote.http.Route.execute (Route.java:68)
at org.openqa.selenium.grid.sessionqueue.NewSessionQueue.execute (NewSessionQueue.java:123)
at org.openqa.selenium.remote.tracing.SpanWrappedHttpHandler.execute (SpanWrappedHttpHandler.java:86)
at org.openqa.selenium.remote.http.Filter$1.execute (Filter.java:64)
at org.openqa.selenium.remote.http.Route$CombinedRoute.handle (Route.java:336)
at org.openqa.selenium.remote.http.Route.execute (Route.java:68)
at org.openqa.selenium.grid.router.Router.execute (Router.java:91)
at org.openqa.selenium.grid.web.CheckOriginHeader.lambda$apply$0 (CheckOriginHeader.java:66)
at org.openqa.selenium.grid.web.CheckContentTypeHeader.lambda$apply$0 (CheckContentTypeHeader.java:70)
at org.openqa.selenium.grid.web.EnsureSpecCompliantResponseHeaders.lambda$apply$0 (EnsureSpecCompliantResponseHeaders.java:34)
at org.openqa.selenium.remote.http.Filter$1.execute (Filter.java:64)
at org.openqa.selenium.remote.http.Route$CombinedRoute.handle (Route.java:336)
at org.openqa.selenium.remote.http.Route.execute (Route.java:68)
at org.openqa.selenium.remote.http.Route$NestedRoute.handle (Route.java:270)
at org.openqa.selenium.remote.http.Route.execute (Route.java:68)
at org.openqa.selenium.remote.http.Route$CombinedRoute.handle (Route.java:336)
at org.openqa.selenium.remote.http.Route.execute (Route.java:68)
at org.openqa.selenium.remote.http.Route$CombinedRoute.handle (Route.java:336)
at org.openqa.selenium.remote.http.Route.execute (Route.java:68)
at org.openqa.selenium.remote.AddWebDriverSpecHeaders.lambda$apply$0 (AddWebDriverSpecHeaders.java:35)
at org.openqa.selenium.remote.ErrorFilter.lambda$apply$0 (ErrorFilter.java:44)
at org.openqa.selenium.remote.http.Filter$1.execute (Filter.java:64)
at org.openqa.selenium.remote.ErrorFilter.lambda$apply$0 (ErrorFilter.java:44)
at org.openqa.selenium.remote.http.Filter$1.execute (Filter.java:64)
at org.openqa.selenium.netty.server.SeleniumHandler.lambda$channelRead0$0 (SeleniumHandler.java:44)
at java.util.concurrent.Executors$RunnableAdapter.call (None:-1)
at java.util.concurrent.FutureTask.run (None:-1)
at java.util.concurrent.ThreadPoolExecutor.runWorker (None:-1)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (None:-1)
at java.lang.Thread.run (None:-1)
已尝试将`seleniumwire_options`中的`addr`改为网格IP(192.168.21.3),但问题未解决,且此时`driver.version`显示unknown,原Selenium代码可正常识别驱动版本。需要修改哪些配置或代码,才能让seleniumwire与原Selenium代码一样正常工作? --- # 解决方案 ### 1. 匹配Selenium Wire与Selenium版本兼容性 你的Selenium版本是4.5.0,需确保安装的Selenium Wire版本与之兼容。建议安装适配Selenium 4.x的版本,执行: ```bash pip install seleniumwire==5.1.0
(可根据实际情况调整版本号,核心是保证两者版本匹配)
2. 调整Selenium Wire代理配置
- 开启自动配置:将
seleniumwire_options中的auto-config设为True,让Selenium Wire自动完成代理配置,避免手动配置错误:self.seleniumwire_options = {'addr': "0.0.0.0", 'auto-config': True} - 使用
0.0.0.0作为监听地址:允许所有网络接口访问代理,避免仅绑定127.0.0.1导致Grid节点无法连接到代理服务。
3. 修正父类初始化参数传递方式
Selenium Wire的Remote类初始化时,可尝试将代理配置直接注入到Chrome选项中,而非单独传递seleniumwire_options:
# 先指定Selenium Wire的端口,默认8080 proxy_port = self.seleniumwire_options.get('port', 8080) self.chrome_options.add_argument(f'--proxy-server=127.0.0.1:{proxy_port}')
或者确保seleniumwire_options参数完整,包含端口等必要信息:
self.seleniumwire_options = { 'addr': '0.0.0.0', 'port': 8080, 'auto-config': True }
4. 检查Selenium Grid网络连通性
- 确保Grid节点所在机器能访问Selenium Wire所在机器的代理端口(比如8080),在Windows防火墙中开放该端口的入站规则。
- 确认Grid节点的配置没有限制外部代理连接。
5. 延迟页面操作时机
将self.get(url)和self.switch_to.default_content()从__init__方法中移出,改为在实例创建后调用的独立方法中执行,避免会话未完全建立就触发页面请求导致超时:
def start_scraping(self): self.get(self.url) self.switch_to.default_content()
实例化后调用:
scraper = Scraping(url, command_executor=COMMAND_EXECUTOR) scraper.start_scraping()
内容的提问来源于stack exchange,提问作者ajust

