Python selenium automation browser

37 views Asked by At

I am working with python selenium.Gologin provide me multiple browses with different proxy.

I want that I can automate multiple browser same time. I used multiprocessing, but it open multiple browser but only one of them work fine and other browser doesn't work. I want That it will automate my all browser in same time.

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

There are 0 answers