1

I want to examine a button that leads to downloading a file. the HTML contains:

<a class="calibre-push-button " href="/calibre/get/EPUB/123/calibre" role="button" title="Download this file" download="a.txt"><svg style="fill: currentColor; height: 2ex; width: 2ex; vertical-align: text-top"><use xlink:href="#icon-cloud-download"></use></svg><span>&nbsp;Download</span></a>

I want to write a python code to download the file (click on the button) and save it. I started working with Selenium, but hit the wall. This is what I've done:

from selenium import webdriver
driver=webdriver.Chrome(executable_path='chromedriver')#,chrome_options=options)
driver.get('http://website.address.com')
gotit = driver.find_element_by_xpath("//a.calibre-push-button [contains(text(),'')]")

I want the gotit element to store the button optionally and allow gotit.click().

Any ideas?

2

1 Answer 1

0

To properly click the button and download the file, you need to perform the following steps:

  1. Locate the button element: You can use the find_element_by_css_selector method to locate the button element based on the class name and text. The updated code would look like this:

    gotit = driver.find_element_by_css_selector('a.calibre-push-button span:contains("Download")')

  2. Click the button: Once you have located the button element, you can use the click method to click the button and initiate the download process.

    gotit.click()

  3. Wait for the download to complete: The download may take some time to complete, so you need to wait for it to finish before you can save the file. You can use the WebDriverWait class and the expected_conditions module to wait for the file to download.

Here's an example:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ... other code ...

# Click the button
gotit.click()

# Wait for the file to download
wait = WebDriverWait(driver, 30)
wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, '.txt')))
  1. Save the file: After the download has completed, you can use the os module to save the file to your desired location.

Here's an example:

import os

# ... other code ...

# Save the file
download_path = '/path/to/download/folder'
for f in os.listdir(download_path):
    if f.endswith('.txt'):
        file_path = os.path.join(download_path, f)
        with open(file_path, 'wb') as f:
            f.write(requests.get(gotit.get_attribute('href')).content)

Here is another solution for downloading the file after clicking the button using Selenium and Requests:

from selenium import webdriver
import requests

driver = webdriver.Chrome(executable_path='chromedriver')
driver.get('http://website.address.com')

gotit = driver.find_element_by_xpath("//a.calibre-push-button [contains(text(),'Download')]")
gotit.click()

file_url = gotit.get_attribute('href')
file_name = gotit.get_attribute('download')
file_path = f"{file_name}"

with open(file_path, 'wb') as f:
    f.write(requests.get(file_url).content)

driver.quit()

This solution opens a Chrome browser, navigates to the specified URL, locates the button element with the given class name and text, clicks it to initiate the file download, retrieves the file URL and name from the button's href and download attributes, and finally writes the file content to the specified file path using the Requests library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.