Сам список содержит несколько тысяч URL-адресов.
Итак, я пытаюсь использовать пул процессов для управления несколькими экземплярами Google Chrome. И мне нужно указать каталог пользователя для доступа к его файлам cookie с помощью аргумента --user-data-dir.
Но я получил сообщение об ошибке:< /p>
Message: session not created
from unknown error: Could not remove old devtools port file. Perhaps the given user-data-dir at C:\Users\ilpla\AppData\Local\Chromium\User Data is still attached to a running Chrome or Chromium process
Есть ли способ открыть несколько экземпляров Google Chrome без возникновения этой ошибки?
Вот код, который я пытаюсь использовать:< /p>
#!/bin/python3
import argparse
import pathlib
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from tqdm import tqdm
from multiprocessing import Pool
from colorama import Fore
# Class for the album
class Album:
"""
Class for the album.
"""
def __init__(self, id, title, productUrl, mediaItemsCount):
self.id = id
self.title = title
self.productUrl = productUrl
self.mediaItemsCount = mediaItemsCount
# Class for the albums list
class AlbumList:
"""
Class for the album list.
"""
def __init__(self):
self.albums = []
def delete_empty_albums(location: pathlib.Path, profile: pathlib.Path, threads: int):
"""
Delete the albums with no items in them.
Parameters:
location: pathlib.Path
The path to the browser location to use.
profile: pathlib.Path
The path to the user profile to use.
threads: int
The number of threads to use.
"""
# Get the empty albums
albums = list_empty_albums()
# Create the pool of drivers
pool = Pool(processes = threads)
try:
# Loop on the empty albums
for album in tqdm(albums.albums,
desc = "Deleting empty albums",
unit = "album",
position = 0,
leave = True):
# If the album has no items, remove it
if album.mediaItemsCount == 0:
# Remove the empty album
try:
tqdm.write(f"""{Fore.BLUE}
tqdm.write(f"\tURL: {album.productUrl}{Fore.RESET}")
# Use the browser to remove the album
pool.apply_async(delete_album,
args = (album.productUrl, location, profile),
callback = lambda _: delete_album_success(album.title, album.id),
error_callback = lambda e: delete_album_error(album.title, album.id, e)
)
except Exception as e:
tqdm.write(f"""{Fore.RED}
tqdm.write(f"\tError: {e}")
tqdm.write(f"\tID: {album.id}")
tqdm.write(f"\tURL: {album.productUrl}{Fore.RESET}")
except Exception as e:
tqdm.write(f"{Fore.RED}
finally:
# Close the pool
pool.close()
pool.join()
def list_empty_albums() -> AlbumList:
"""
List all the empty albums.
"""
# Initialize the album list
album_list = AlbumList()
# ...
def delete_album(album_productUrl: str, location: pathlib.Path, profile: pathlib.Path) -> bool:
"""
Delete the album with the given product URL.
"""
driver = browser_driver(location, profile)
try:
# Open the album page
driver.get(album_productUrl)
# Wait for the page to load
driver.implicitly_wait(10)
# Click on the top right button
ActionChains(driver).move_by_offset(750, 30).click().perform()
time.sleep(0.2)
# Click on the "Delete the album" menu item
ActionChains(driver).move_by_offset(0, 60).click().perform()
time.sleep(0.2)
# Click on the "Delete" button
ActionChains(driver).move_by_offset(-150, 200).click().perform()
time.sleep(1)
# Wait for the page to load
driver.implicitly_wait(10)
return True
finally:
# Close the browser
driver.quit()
def browser_driver(location: pathlib.Path, profile: pathlib.Path) -> webdriver.Chrome:
"""
Create a browser driver.
"""
# Create the driver for the browser
options = Options()
if location:
options.binary_location = str(location)
options.headless = True
# Set the user data location
options.add_argument(f"--user-data-dir={profile}")
# Disable the browser logs in the console
options.add_argument("--silent")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
# Set the window size
options.add_argument("--window-size=800,600")
driver = webdriver.Chrome(options = options)
driver.set_window_size(800, 600)
return driver
def delete_album_success(album_title: str, album_id: str):
"""
Called when album deletion is successful.
"""
tqdm.write(f"""{Fore.GREEN}
tqdm.write(f"\tID: {album_id}{Fore.RESET}")
def delete_album_error(album_title: str, album_id: str, error: Exception):
"""
Called when album deletion fails.
"""
tqdm.write(f"""{Fore.RED}
tqdm.write(f"\tID: {album_id}{Fore.RESET}")
tqdm.write(f"\tError: {error}{Fore.RESET}")
if __name__ == '__main__':
# Command line options
parser = argparse.ArgumentParser(description = "Delete empty albums")
parser.add_argument("-l", "--location",
required = False,
type = pathlib.Path,
help = "Path to the browser location to use. Work only with Google Chrome or Chromium.")
parser.add_argument("-p", "--profile",
required = False,
type = pathlib.Path,
help = "Path to the user profile to use.")
parser.add_argument("-t", "--threads",
required = False,
type = int,
default = 5,
help = "Number of threads to use (default: 5).")
args = parser.parse_args()
# ...
delete_empty_albums(args.location, args.profile, args.threads)
Подробнее здесь: https://stackoverflow.com/questions/793 ... r-data-dir