Ogre55
Ogre55

Reputation: 237

Cannot download anything with Python Selenium Chrome Ubuntu 20.04

I am having trouble getting files to download with Selenium Chrome in Python with headless mode. I have checked a few answers on here and most of them are the same. they talk about adding prefs and experimental options.

Download with python selenium

Selenium Webdriver: How to Download a PDF File with Python?

Downloading a PDF using Selenium, Chrome and Python

Downloading with chrome headless and selenium

None of these has helped, and i am starting to wonder if it has something to do with my environment.

Ubuntu: 20.04

Python: 3.8.2

Chromeium-browser: Chromium 80.0.3987.162 snap

Cromium-driver: ChromeDriver 80.0.3987.162 (f2c5dd6138153bb0658091205bd1a1717f16081a-refs/branch-heads/3987@{#1034})

Here is a basic piece of sample code that is not working. it completes without any errors, but the file never downloads.

#!/usr/bin/env python3

import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

IMG_DIR = '/images/'
if not os.path.exists(IMG_DIR):
  os.makedirs(IMG_DIR)

options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--window-size=1920,1080")

prefs = {"download.default_directory" : IMG_DIR}
options.add_experimental_option("prefs",prefs)

driver = webdriver.Chrome(options=options, executable_path='chromedriver')

driver.get('https://www.thinkbroadband.com/download')

#Find the Extra Small file on port 80
download_button = driver.find_element_by_xpath('//*[@id="main-col"]/div/div/div[8]/p[2]/a[1]')

driver.execute_script("arguments[0].scrollIntoView();", download_button)
download_button.click()
time.sleep(10)
driver.save_screenshot('{}page.png'.format(IMG_DIR))

driver.get('chrome://downloads')
time.sleep(10)
driver.switch_to.window(driver.window_handles[-1])
driver.save_screenshot('{}downloads.png'.format(IMG_DIR))

driver.close()
driver.quit()

I have attached both screenshots, but the downloads page always comes out as a blank white page.

Test file download site:

test page after button click

Chrome Download page:

Chrome Download page

Please help!

UPDATE: I have removed the snap versions of chrome and it seems to be working better. i can download files to a custom directory. must be a bug with the snap version.

Upvotes: 0

Views: 1193

Answers (1)

Shaheed Haque
Shaheed Haque

Reputation: 723

I just filed a bug on this. As you can see from the write-up, the problem is that the download folder that the Snap uses is pointing into the Snap setup, and completely ignoring your IMG_DIR setting.

You should be able to confirm this by stopping your test at the point of the download, and looking into the browser "Downloads" directly for the folder where the download is destined.

Upvotes: 0

Related Questions