pythongoogle-chromeselenium-webdriver

Trying to use chrome_undetected selenium driver and portable chrome


I have the code below and I have chrome portable installed at C:\temp\GoogleChromePortable64\App\Chrome-bin\chrome.exe how do I tell undetected_chromedriver to use it?

# Import the required libraries
import undetected_chromedriver as uc
from selenium.webdriver.chrome.service import Service as ChromeService
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.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import time

# Define Chrome options
options = uc.ChromeOptions()

# Set headless to False to run in non-headless mode
options.headless = False

# Chrome driver
driver = uc.Chrome(use_subprocess=True, options=options)

# Go to the desired URL
driver.get("https://library.usask.ca/#gsc.tab=0")

# Wait until the input field is visible
wait = WebDriverWait(driver, 10)
q_field = wait.until(EC.presence_of_element_located((By.ID, "primoQueryTemp")))

# Use JavaScript to focus the element
driver.execute_script("arguments[0].focus();", q_field)

# Initiate typing into the field
q_field.send_keys("_")

# Click the field to trigger any other events
q_field.click()

# Keep the browser open for observation
time.sleep(566)

# Close the driver
driver.quit()

Solution

  • Usually the user data is stored in the data directory, not in the App folder for the PortableApps.

    Add the following options.

    options.binary_location = "location of your portable browser binary location"
    options.add_argument("--user-data-dir=location of your user data")
    

    You don't need to add the binary location. But sometimes the browser crashes when your profile data doesn't match properly and you are running the application already with the same profile. So, It is better to use the portable browsers binary with the portable browsers data. And don't use the profile when running the script.

    So the code looks like:

    # Import the required libraries
    import undetected_chromedriver as uc
    from selenium.webdriver.chrome.service import Service as ChromeService
    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.common.keys import Keys
    # from webdriver_manager.chrome import ChromeDriverManager
    import time
    
    # Define Chrome options
    options = uc.ChromeOptions()
    
    # Set headless to False to run in non-headless mode
    options.headless = False
    # Set the binary location of the browser
    options.binary_location = "C:\\temp\\GoogleChromePortable64\\App\\Chrome-bin\\chrome.exe"
    # Set the user data directory to save user info
    options.add_argument("--user-data-dir=C:\\temp\\GoogleChromePortable64\\Data\\profile")
    # Chrome driver
    driver = uc.Chrome(use_subprocess=True, options=options)
    
    # Go to the desired URL
    driver.get("https://library.usask.ca/#gsc.tab=0")
    
    # Wait until the input field is visible
    wait = WebDriverWait(driver, 10)
    q_field = wait.until(EC.presence_of_element_located((By.ID, "primoQueryTemp")))
    
    # Use JavaScript to focus the element
    driver.execute_script("arguments[0].focus();", q_field)
    
    # Initiate typing into the field
    q_field.send_keys("_")
    
    # Click the field to trigger any other events
    q_field.click()
    
    # Keep the browser open for observation
    time.sleep(60)
    
    # Close the driver
    driver.quit()