尝试重新打开dri时Selenium抛出ConnectionRefusedError

2024-10-01 17:26:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中使用带有Selenium的Chrome webdriver,在试图关闭/重新打开我的驱动程序时遇到了问题。从谷歌,我想我可能把端口号弄乱了?但我不知道如何或是否能解决这个问题。在

下面是我尝试实现的代码:

def refreshDriver(driver, url):
print('refreshing driver')
driver.quit()
driver = webdriver.Chrome()
driver.set_window_size(1680,1050)
trycount=0
success=0
while trycount<20 and success==0:
    try:
        driver.get(url)
        success=1
    except:
        sleep(3)
        trycount = trycount + 1
        if trycount==1:
            print(url)
        print('Retrying Connection... ',trycount)
sleep(.5)
return driver

这是我在调用此函数后执行代码行时遇到的错误:

^{pr2}$

出什么问题了?取决于我把代码片段放在什么地方,它是有效的还是抛出这个错误,我不知道为什么。我完全不知所措。如果是因为端口号正在更改,我该如何修复?在

谢谢!在

如果你想知道我为什么要关闭并重新打开我的webdriver,那是因为我的脚本很长,并且积累了大量的Chrome内存,导致它停止运行。我试图通过在访问任何特别大的页面后重新启动chrome来缓解这种情况。在


Tags: 代码urldefdriverselenium错误驱动程序sleep

热门问题