Python Selenium 自动化浏览器

0 投票
0 回答
32 浏览
提问于 2025-04-12 01:12

我正在使用Python的Selenium库。Gologin给我提供了多个浏览器,每个浏览器都有不同的代理。

我想要能够同时自动化多个浏览器。我尝试了多进程的方法,但虽然可以打开多个浏览器,但只有一个能正常工作,其他的浏览器却不行。我希望能够同时自动化所有的浏览器。

import time
from sys import platform
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, WebDriverException
from gologin import GoLogin
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
import os
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from multiprocessing import Pool

os.system('cls')

def process_profile(profile_id):
    try:
        # Attempt to login
        gl = GoLogin({
            "token": "Token",
            "profile_id": profile_id,
        })
        debugger_address = gl.start()

        if platform == "win32":
            chrome_driver_path = r".\chromedriver.exe"

        service = Service(executable_path=chrome_driver_path)

        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("debuggerAddress", debugger_address)

        driver = webdriver.Chrome(service=service, options=chrome_options)
        driver.get("https://example.com")

        # Retry logic for the first URL
        retry_count = 0
        max_retries = 2
        while retry_count < max_retries:
            try:
                follow_link_button = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Follow Link']")))
                follow_link_button.click()
                break  # Break out of the loop if successful
            except (TimeoutException, WebDriverException) as e:
                print(f"Error loading URL (retry {retry_count + 1}/{max_retries}): {e}")
                retry_count += 1
                time.sleep(0)  # Wait before retrying

        # Retry logic for the second URL
        retry_count = 0
        max_retries = 2
        while retry_count < max_retries:
            try:
                new_page_link = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div[1]/div/div/div[1]/div[1]/div[1]/div/div/div[2]/a")))
                new_page_link.click()
                WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
                break  # Break out of the loop if successful
            except (TimeoutException, WebDriverException) as e:
                print(f"Error loading URL (retry {retry_count + 1}/{max_retries}): {e}")
                retry_count += 1
                time.sleep(0)  # Wait before retrying

        # Wait for page load
        # WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
        delete_cache(driver)

        # Close the browser
        driver.quit()
        gl.stop()

    except Exception as e:
        print(f"An unexpected error occurred for profile ID {profile_id}: {e}")
        # Write the profile ID to an error file
        with open("error_profiles.txt", "a") as error_file:
            error_file.write(profile_id + "\n")

def delete_cache(driver):
    driver.execute_script("window.open('')")  # Create a separate tab than the main one
    driver.switch_to.window(driver.window_handles[-1])  # Switch window to the second tab
    driver.get('chrome://settings/clearBrowserData')  # Open your chrome settings.
    time.sleep(1)
    actions = ActionChains(driver)
    actions.key_down(Keys.SHIFT).send_keys(Keys.TAB * 6).key_up(Keys.SHIFT)  # Select "all time" browsing data
    actions.perform()
    time.sleep(0)
    actions.send_keys(Keys.DOWN * 5 + Keys.TAB * 7 + Keys.ENTER)  # Click on "clear data" button
    actions.perform()
    time.sleep(3)
    print("Succesfully cleared the browsing data")

if __name__ == "__main__":
    # Attempt to login
    gl = GoLogin({
        "token": "Token"
    })

    # Get the profiles from the response
    profiles = gl.profiles()['profiles']

    # Extract the profile IDs
    profile_ids = [profile['id'] for profile in profiles]

    # Print the profile IDs
    print(profile_ids)

    # Set the maximum number of processes
    max_processes = 5

    # Create a multiprocessing pool with the specified number of workers
    with Pool(max_processes) as pool:
        # Start the processes, each with a different profile ID
        pool.map(process_profile, profile_ids)

0 个回答

暂无回答

撰写回答