Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Download Path Not Working with undetected_chrome #1935

Open
babarali539 opened this issue Jun 27, 2024 · 8 comments
Open

Custom Download Path Not Working with undetected_chrome #1935

babarali539 opened this issue Jun 27, 2024 · 8 comments

Comments

@babarali539
Copy link

I was previously using the default WebDriver from Selenium for web scraping, and I had set a custom download path that was working perfectly. However, after switching to undetected_chrome, the download files are now being saved to the default downloads directory instead of the custom path I specified.

Here is my method to initialize driver:

def initialize_driver(storage_directory):
    options = uc.ChromeOptions()
    options.add_argument('--headless=new')
    options.add_argument('--no-sandbox')

    prefs = {
        "download.default_directory": storage_directory,
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing.enabled": True,
        "profile.default_content_setting_values.automatic_downloads": 1
    }
    options.add_experimental_option("prefs", prefs)

    driver = uc.Chrome(options=options)
    return driver

And here is the method to set custom download path

def ensure_storage_directory():
    env = os.getenv('ENVIRONMENT', 'development')
    if env == 'production':
        storage_directory = "/tmp/storage"
    else:
        storage_subdirectory = 'storage'
        current_directory = os.getcwd()
        storage_directory = os.path.join(current_directory, storage_subdirectory)

    if not os.path.exists(storage_directory):
        os.makedirs(storage_directory)
    return storage_directory

Environment
OS: ubuntu-22.04
Python Version: 3.10
undetected_chrome Version: 3.5.5

Any help or guidance on how to resolve this issue would be greatly appreciated. Thank you!

@weyseley
Copy link

i have the same problem

@tiborrr
Copy link

tiborrr commented Jul 10, 2024

I have a workaround. I only use the undetected chrome driver to log in. Then afterwards I copy the session to a requests.Session and do all download stuff from there.

Something like the following:

from selenium.webdriver import Chrome
from requests import Session
from urllib3.util import Timeout
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter

def get_session(browser: Chrome) -> Session:
    # Enable the Network domain in the Chrome DevTools Protocol
    browser.execute_cdp_cmd("Network.enable", {})

    # Attempt to find the cookie from the list of all cookies, try up to three times with a 3-second wait between attempts
    access_token = None

    timeout_in_s = 10
    timeout = time.time() + timeout_in_s
    while not access_token:
        # Retrieve all cookies through the CDP command
        access_token = browser.execute_script("return sessionStorage.getItem('access_token');")

        if isinstance(access_token, str):
            access_token = access_token.strip('"')
            break
        if time.time() > timeout:
            raise Exception(f"Access token not found in browser session after waiting {timeout_in_s} seconds.")
        time.sleep(0.5)

    session = Session()
    session.headers.update({"Authorization": f"Bearer {access_token}"})

    retries = Retry(
        total=20,
        backoff_factor=0.1,
        status_forcelist=[400, 500]
    )

    session.mount('https://', HTTPAdapter(max_retries=retries))
    session.mount('http://', HTTPAdapter(max_retries=retries))

    return session

@siddagra
Copy link

siddagra commented Jul 11, 2024

How did you even get downloading to work?

I keep getting this:

raise WebDriverException("You must enable downloads in order to work with downloadable files.")

selenium.common.exceptions.WebDriverException: Message: You must enable downloads in order to work with downloadable files.

@bulutg
Copy link

bulutg commented Jul 24, 2024

I have the same problem.

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions from invalid argument: unrecognized chrome option: prefs

@babarali539
Copy link
Author

I got it working now. Just needed to add the following:

params = {
        "behavior": "allow",
        "downloadPath": storage_directory
    }
driver.execute_cdp_cmd("Page.setDownloadBehavior", params)

@nk9
Copy link

nk9 commented Jul 28, 2024

I got it working now. Just needed to add the following:

params = {
        "behavior": "allow",
        "downloadPath": storage_directory
    }
driver.execute_cdp_cmd("Page.setDownloadBehavior", params)

This worked, thanks! Just make sure that the storage_directory is a string, not a Path.

@WebForks
Copy link

WebForks commented Aug 1, 2024

I got it working now. Just needed to add the following:

params = {
        "behavior": "allow",
        "downloadPath": storage_directory
    }
driver.execute_cdp_cmd("Page.setDownloadBehavior", params)

Doesn't seem to work now :(

@weyseley
Copy link

weyseley commented Aug 2, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants