0

I want to download files from a website.

The website has a file download button.

But when I click the download button by button.click(), the file doesn't download on Colaboratory. (It works at Juypyter Notebook.)

I googled and found a solution. My code applying the solution was as follows:

!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

# solution
prefs = {'download.default_directory' : "/some/path"}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome('chromedriver',options=options)

# Download code
xpath = '''//*[@id="glopopd_excel"]/span'''
element_get_excel = driver.find_element_by_xpath(xpath).click()

But I can't find downloaded files anywhere.

Do I have to type 'GoogleDrive path' on prefs? (But it didn't work.)

If not, how can I download files on a click event using Selenium on Colaboratory?

qss_b
  • 33
  • 6

1 Answers1

1

Yes, you have to type GoogleDrive path but not normally, as you expect. You have to first mount/ask permission to read and write file, using:

from google.colab import drive
drive.mount('/content/gdrive')

Run this cell first, it will ask for the google drive permission, complete this steps accordingly and now you can do anything with google drive. Now it is same as our local machine. If you want then you may change directory now, using !cd <path>. I don't know how you are trying to download file but now it is same as local machine. You can visit these file in provided directory of google drive, after download.

First completely run that cell, and run your file downloader cell. Just doing this should work for you.

You can visit here for more/detail information.

Edit:
When you are saving files, simply specify the Google Drive path for saving the file. When using large files, Colab sometimes syncs the VM and Drive asynchronously. To force the sync, simply run:

from google.colab import drive
drive.flush_and_unmount()
imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • Thanks for your attention. I type '/content/drive/Mydrive' on 'download.default_directory'. But it doesn't work :( I also googled , but can't find answer. What am I doing wrong? – qss_b Jul 20 '21 at 18:24
  • Can you provide me, how you are trying to download file/code that do download? – imxitiz Jul 20 '21 at 18:29
  • I don't know if I understood your comment correctly, the website has a download button. When I click this button, xlxs file is downloaded. Download code is as follows; – qss_b Jul 20 '21 at 18:42
  • 1. xpath = '''//*[@id="glopopd_excel"]/span''' 2. element_get_excel = driver.find_element_by_xpath(xpath).click() – qss_b Jul 20 '21 at 18:42
  • Oh! Sorry! My bad. I misunderstood your question. I thought you are asking for to download file using python but you are doing it in website download button. Mounting doesn't work for you, right? You have checked for file in GoogleDrive right? – imxitiz Jul 20 '21 at 18:43
  • Mounting works well. But when I execute code, I can't find dowloaded files in Googledrive. I think the path setting is wrong, but I don't know how to solve it. – qss_b Jul 20 '21 at 18:53
  • 1
    please give all detail, like `My_local_Download_path` value, where you have mounted. Or if it saved file in colab directory only. You can check that too. – imxitiz Jul 20 '21 at 18:58
  • Check the working directory? And check there in googledrive, if it is under colab the you may have to copy/paste in googledrive. To see it in google drive. – imxitiz Jul 20 '21 at 19:04
  • I am sorry to bother you. Here's the solution link that I referred. [link](https://stackoverflow.com/questions/18026391/selenium-webdriver-in-python-files-download-directory-change-in-chrome-prefere) Originally, the path setting should be like "C:\Users\..." but I changed path to `Googledrive path` because i am using colab. – qss_b Jul 20 '21 at 19:09
  • Then answer your own question. Don't be sorry! – imxitiz Jul 20 '21 at 19:11