在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稳定版本:
Google Chrome 80.0.3987.149
在这里茫然。任何帮助表示赞赏。谢谢。