Vladimir Vargas
Vladimir Vargas

Reputation: 1834

Download with python selenium

I want to download a file from a website. In the website I click on a button that opens a small sub-window, which has a button that downloads a file to the directory path_herewhen clicked. This is my solution:

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

chrome_options = Options()

chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_experimental_option("prefs", {
  "download.default_directory": r'path_here',
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True
})
driver = webdriver.Chrome("./chromedriver", options=chrome_options)

website = "https://www.chess.com/ccc"
driver.get(website)  # loads the page
# This closes a sub-window that opens automatically
element = driver.find_element_by_class_name("form-button-component")
element.click()

driver.find_element_by_class_name("icon-download").click()
download = driver.find_element_by_class_name('download-pgn')
# Click to download
download.find_element_by_class_name("btn").click()

This should work, but does not download the file, as I expected. I add a screenshot for completeness:

description

The button is Download Game (PGN), whose text is obtained by print(download.find_element_by_class_name("btn").text)

Upvotes: 2

Views: 1428

Answers (1)

Thanthu
Thanthu

Reputation: 5108

In the website I click on a button that opens a small sub-window

Here you've mentioned that you are opening a new sub window where you've to click the button to download. But you are not switching to that window. Hence not able to find that element.

Use driver.window_handles to get handle to the opened window and switch to that window using driver.switch_to_window() then try clicking on the button to download.

You can see how to handle multiple windows in python selenium in this stackoverflow link.

EDIT:

So it seems there were some issues in your code. Like the locator for download buttons next to chess board and the one after that were incorrect. I've corrected the locator's with proper xpath and also made little change in chrome_options. You just have to change the download.defualt_directory to a path in you machine and the below code will work:

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

chrome_options = Options()

chrome_options.add_experimental_option("prefs", {
  "download.default_directory": r"C:\Users\Thanthu Nair\Downloads\Test",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True
})
driver = webdriver.Chrome("./chromedriver", options=chrome_options)

website = "https://www.chess.com/ccc"
driver.get(website)  # loads the page
driver.maximize_window()

# This closes a sub-window that opens automatically
element = driver.find_element_by_class_name("form-button-component")
element.click()

download = driver.find_element_by_xpath("//i[@title='Download']")
download.click()

# Click to download
download.find_element_by_xpath("//button[normalize-space()='Download Game (PGN)']").click()

Upvotes: 2

Related Questions