首页 > 解决方案 > selenium.common.exceptions.WebDriverException:消息:未知错误:DevToolsActivePort 文件不存在

问题描述

我在 Ubuntu 上的 Python 脚本中使用 Selenium/Webdriver。下面的代码在 for 循环中通过大约 25 次迭代成功,然后得到错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist

我研究了这个问题,并确保我遵循了更新 Chromedriver 和 Google Chrome 的建议,并在 Chrome 选项中添加了 disable-dev-usage 参数。这是完整的代码:

options = webdriver.ChromeOptions() 
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1420,1080')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
options.add_argument("--disable-notifications")
options.add_experimental_option('useAutomationExtension', False)
options.binary_location='/usr/bin/google-chrome-stable'
chrome_driver_binary = "/usr/bin/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)



# Get base url
base_url = 'https://www.bandsintown.com/?place_id=ChIJOwg_06VPwokRYv534QaPC8g&page='

events = []
eventContainerBucket = []

for i in range(1,55):
    driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
    #cycle through pages in range
    driver.get(base_url + str(i))
    pageURL = base_url + str(i)
    print(pageURL)
    
    # get events links
    event_list = driver.find_elements_by_css_selector('div[class^=_3buUBPWBhUz9KBQqgXm-gf] a[class^=_3UX9sLQPbNUbfbaigy35li]')
   
    # collect href attribute of events in even_list
    events.extend(list(event.get_attribute("href") for event in event_list))

    driver.close()


# iterate through all events and open them.
item = {}
allEvents = []
for event in events:
  
    driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
    driver.get(event)

    #Do some things here

driver.close()  

Chrome驱动程序版本:

ChromeDriver 80.0.3987.149 (5f4eb224680e5d7dca88504586e9fd951840cac6-refs/branch-heads/3987_137@{#16})

谷歌浏览器稳定版:

Google Chrome 80.0.3987.149 

接下来我可以尝试什么?

标签: pythonseleniumgoogle-chromeselenium-chromedriverubuntu-14.04

解决方案


推荐阅读