selenium.common.exceptions.WebDriverException:消息:未知错误:DevToolsActivePort文件不存在

2024-04-18 16:28:03 发布

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

我正在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()  

Chromedriver版本:

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

谷歌浏览器稳定版:

Google Chrome 80.0.3987.149 

下一步我能试试什么


Tags: ineventaddurlforbasedriverchrome