我正在将Selenium python与chromedriver一起使用,我收到了关于不兼容的chromedriver和chrome版本的常见错误:
session not created
from disconnected: unable to connect to renderer
(Session info: chrome=96.0.4664.110)
我去下载chromedriver 96,但我一直得到同样的错误。这是我检查chromedriver版本时的输出:
> chromedriver --version
ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947})
以下是我如何在代码中初始化驱动程序:
options = Options()
port = '8888'
options.add_argument('--remote-debugging-port=' + port)
options.add_argument('headless')
try:
driver = webdriver.Chrome(options=options)
except:
...
编辑:在将Selenium更新到4.1.0版本后,我现在得到了一个带有相同错误的堆栈:
session not created
from disconnected: unable to connect to renderer
(Session info: chrome=96.0.4664.110)
Stacktrace:
Backtrace:
Ordinal0 [0x00916903+2517251]
Ordinal0 [0x008AF8E1+2095329]
Ordinal0 [0x007B2848+1058888]
Ordinal0 [0x007A376E+997230]
Ordinal0 [0x007B3A60+1063520]
Ordinal0 [0x007FBA7A+1358458]
Ordinal0 [0x007FA71A+1353498]
Ordinal0 [0x007F639B+1336219]
Ordinal0 [0x007D27A7+1189799]
Ordinal0 [0x007D3609+1193481]
GetHandleVerifier [0x00AA5904+1577972]
GetHandleVerifier [0x00B50B97+2279047]
GetHandleVerifier [0x009A6D09+534521]
GetHandleVerifier [0x009A5DB9+530601]
Ordinal0 [0x008B4FF9+2117625]
Ordinal0 [0x008B98A8+2136232]
Ordinal0 [0x008B99E2+2136546]
Ordinal0 [0x008C3541+2176321]
BaseThreadInitThunk [0x76ACFA29+25]
RtlGetAppContainerNamedObjectPath [0x77A17A9E+286]
RtlGetAppContainerNamedObjectPath [0x77A17A6E+238]
我认为问题与我在无头模式下启动驱动程序有关,并且当程序停止时,它不会自行停止,这导致程序在再次启动时崩溃。
解决方案:在程序结束时使用driver.quit()