如何解决Selenium错误消息:试图在没有建立连接的情况下运行命令?

t9aqgxwy  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(119)

我正在尝试使用Selenium(Python)从许多页面中获取文本出现的频率。在大约1个小时的获取之后,这个错误不断出现:
尝试在不建立连接的情况下运行命令
在此行:
获取驱动程序(url)
这可能是什么原因/解决方案?下面是我的代码,(我反复调用的函数)。

options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options, executable_path = 'C:/Program Files/Mozilla Firefox/geckodriver')

def get_attack_mention_count(mention,srch_resultsQ):
    #harvesting
    totalQ=0
    for result in srch_resultsQ:
        url=result['link'][2]['@href']
        #page = requests.get(url)

        driver.get(url)##################ERROR HERE ##########################################
        print("URL:",url)
        time.sleep(3)
        delay = 20 # seconds
        try:
            myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.CLASS_NAME, 'Highlight-module__1p2SO')))
            print ("Page is ready!")
        except TimeoutException:
            print ("Loading took too much time!")
        except:
            print ("exception raised")
            continue
        text_elements =  driver.find_elements_by_class_name("Highlight-module__1p2SO");
        for element in text_elements:
            element_text=element.text
            count = sum(element_text.replace("-"," ").upper().count(x.upper().replace("-"," ")) for x in mention)
            print("count:",count)
            totalQ+=count
        print("end of paper...total so far for ",mention,": ",totalQ)
        #driver.close()
        #sys.exit()
    print ("total occurances= ",totalQ)
    return totalQ
s6fujrry

s6fujrry1#

这通常是由于频繁使用driver.close()造成的

相关问题